Home > Net >  Simple tag replacement with BeautifulSoup
Simple tag replacement with BeautifulSoup

Time:05-16

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.

CodePudding user response:

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