这将
<br>在每个
<a>...</a>元素的末尾插入一个标签:
from BeautifulSoup import BeautifulSoup, Tag# ....soup = BeautifulSoup(data)for a in soup.findAll('a'): a.parent.insert(a.parent.index(a)+1, Tag(soup, 'br'))您不能使用,
soup.findAll(tag = '</a>')因为BeautifulSoup不会单独对end标签进行操作-它们被视为同一元素的一部分。
如果要按照注释中的要求将
<a>元素放入元素内
<p>,则可以使用以下命令:
for a in soup.findAll('a'): p = Tag(soup, 'p') #create a P element a.replaceWith(p) #Put it where the A element is p.insert(0, a) #put the A element inside the P (between <p> and </p>)同样,您不必分别创建
<p>和,
</p>因为它们是同一事物的一部分。



