Skip to content Skip to sidebar Skip to footer

Beautiful Soup 4: How To Replace A Tag With Text And Another Tag?

I want to replace a tag with another tag and put the contents of the old tag before the new one. For example: I want to change this:

This is the

Solution 1:

The idea is to find every span tag with id attribute (span[id]CSS Selector), use insert_after() to insert a sup tag after it and unwrap() to replace the tag with it's contents:

from bs4 import BeautifulSoup

data = """
<html>
<body>
<p>This is the <span id="1">first</span> paragraph</p>
<p>This is the <span id="2">second</span> paragraph</p>
</body>
</html>
"""

soup = BeautifulSoup(data)
for span in soup.select('span[id]'):
    # insert sup tag after the span
    sup = soup.new_tag('sup')
    sup.string = span['id']
    span.insert_after(sup)

    # replace the span tag with it's contents
    span.unwrap()

print soup

Prints:

<html>
<body>
<p>This is the first<sup>1</sup> paragraph</p>
<p>This is the second<sup>2</sup> paragraph</p>
</body>
</html>

Post a Comment for "Beautiful Soup 4: How To Replace A Tag With Text And Another Tag?"