我想使用bs4的insert_after()函数,但是我不明白为什么在这种情况下它只会在第二个标签后面插入。为什么不是每一个标签后面都插入呢?如果我改为插入一些文本,例如代替标签,则会在每个链接后面插入它。
请问如何使用这个函数在每个标签后插入内容?
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p><a href="http://www.foo.com">this if foo</a><a href="http://www.bar.com">this if bar</a></p>')
b = soup.new_tag('b')
for link_tag in soup.findAll('a'):
link_tag.insert_after(b)
print soup
输出:
<html>
<p>
<a href="http://www.foo.com">
this if foo
</a>
<a href="http://www.bar.com">
this if bar
</a>
<b>
</b>
</p>
</body>
</html>
请问如何使用这个函数在每个标签后插入内容?