Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simple tag replacement with BeautifulSoup

I have a very simple tag replacement problem that I’m trying to solve with BeatifulSoup’s replace_with method but I’m having trouble understanding how this is supposed to work. I have the string '<b>This is text</b>' and I’d like to simply convert this to '<bold>This is text</bold>'. It seems like bs4’s replace_with command should be able to do this, but it’s not working like I’d expect. I tried (among some other variants) things like this:

>>> a = '<b>This is text</b>'
>>> soup = BeautifulSoup(a, 'html.parser')
>>> new_tag = soup.new_tag('bold')
>>> soup.b.replace_with(new_tag)
<b>This is text</b>
>>> soup
<bold></bold>

As you can see, the tags got replaced but then I lost the text. I can do this sort of thing with a string replacement but I’d really like to understand why this doesn’t work as I tend to run into similar issues with bs4 with other methods and I must misunderstand something fundamental here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With .replace_with you’re replacing the whole tag with new one – and the new one doesn’t have any content(text). Try instead:

a = "<b>This is text</b>"
soup = BeautifulSoup(a, "html.parser")

soup.b.name = "bold"

print(soup)

Prints:

<bold>This is text</bold>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading