Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a tag after a link with BeautifulSoup

Starting from an Html input like this:

<p>
<a href="http://www.foo.com">this if foo</a>
<a href="http://www.bar.com">this if bar</a>
</p>

using BeautifulSoup, i would like to change this Html in:

<p>
<a href="http://www.foo.com">this if foo</a><b>OK</b>
<a href="http://www.bar.com">this if bar</a><b>OK</b>
</p>

Is it possible to do this using BeautifulSoup?

Something like:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)
for link_tag in soup.findAll('a'):
    link_tag = link_tag + '<b>OK</b>' #This obviously does not work
like image 999
systempuntoout Avatar asked May 25 '10 20:05

systempuntoout


2 Answers

You can use BeautifulSoup's insert to add the element in the right place:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)

for link_tag in soup.findAll('a'):
    link_tag_idx = link_tag.parent.contents.index(link_tag)
    link_tag.parent.insert(link_tag_idx + 1, '<b>OK</b>')

This works for the example you give, though I'm not sure it's the only or most efficient method.

like image 123
tcarobruce Avatar answered Oct 10 '22 04:10

tcarobruce


You have the right idea. Just match up the types, and do replaceWith.

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)
for link_tag in soup.findAll('a'):
    link_tag.replaceWith( link_tag.prettify() + '<b>OK</b>' )
print soup

should give you:

<p>
 <a href="http://www.foo.com">
this if foo
</a>
<b>OK</b>
 <a href="http://www.bar.com">
this if bar
</a>
<b>OK</b>
</p>
like image 25
Jon W Avatar answered Oct 10 '22 04:10

Jon W