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?
source share