0
votes

Using beautifulsoup 4, how do I edit a value based on a name? Given I know name = "foo" (constant and unique), how do I read the value "bar" (unknown at runtime), and change it to a new string, "baz"?

I can read the line using soup.find_all(text="foo"), but I can't figure out how to conveniently read the value, or change the string "bar" --> "baz"

XML Excerpt:

<djmacros>
  <macro name="foo" value="bar">
    <description>foobar</description>
  </macro>
</djmacros>
1

1 Answers

1
votes

I haven't tried it, but I would suspect something like this may work:

foo_element = soup.find('macro', attrs={"name" : "foo"}) # returns the foo element
foo_element['value'] = "baz" # changes the value attribute