Beautiful Soup 4: How to replace a tag with text and another tag?

I want to replace the tag with another tag and put the contents of the old tag in front of the new one. For instance:

I want to change this:

<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> 

in it:

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

I can easily find all spans with find_all() , get the number from the id attribute and replace one tag with another tag with replace_with() , but how can I replace the tag with text and a new tag or insert text before the replaced tag?

+6
source share
1 answer

The idea is to find each span tag with the id attribute ( span[id] CSS selector ), use insert_after() to insert the sup tag after it and unwrap() replace the tag with content:

 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 contents span.unwrap() print soup 

Print

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

Source: https://habr.com/ru/post/978360/


All Articles