views:

44

answers:

2

Starting from an Html input like this:

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

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

<p>
<a href="http://www.foo.com"&gt;this if foo</a><b>OK</b>
<a href="http://www.bar.com"&gt;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
+2  A: 

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"&gt;
this if foo
</a>
<b>OK</b>
 <a href="http://www.bar.com"&gt;
this if bar
</a>
<b>OK</b>
</p>
Jweede
+2  A: 

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.

tcarobruce