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_ta开发者_运维百科g + '<b>OK</b>' #This obviously does not work
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.
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>
精彩评论