Home Python function to replace all instances of a tag with another
Reply: 0

Python function to replace all instances of a tag with another

user1327 Published in July 22, 2018, 10:25 am

How would I go about writing a function (with BeautifulSoup or otherwise) that would replace all instances of one HTML tag with another. For example:

text = "<p>this is some text<p><bad class='foo' data-foo='bar'> with some tags</bad><span>that I would</span><bad>like to replace</bad>"
new_text = replace_tags(text, "bad", "p")
print(new_text)  # "<p>this is some text<p><p class='foo' data-foo='bar'> with some tags</p><span>that I would</span><p>like to replace</p>"

I tried this, but preserving the attributes of each tag is a challenge:

def replace_tags(string, old_tag, new_tag):
  soup = BeautifulSoup(string, "html.parser")
  nodes = soup.findAll(old_tag)
  for node in nodes:
      new_content = BeautifulSoup("<{0}>{1}</{0}".format(
          new_tag, node.contents[0],
  string = soup.body.contents[0]
  return string

Any idea how I could just replace the tag element itself in the soup? Or, even better, does anyone know of a library/utility function that'll handle this more robustly than something I'd write?

Thank you!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318408 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO