PageElement.extract() 方法将当前tag移除文档树,并作为方法结果返回:
去除指定标签from bs4 import BeautifulSoup
#去除属性ul
[s.extract() for s in soup("ul")]
# 去除属性svg
[s.extract() for s in soup("svg")]
# 去除属性script
[s.extract() for s in soup("script")]
# 去除
[s.extract() for s in soup.find_all("sup", {"class": "sup--normal"})]
去除注释from bs4 import BeautifulSoup, Comment
#去除注释
comments = soup.findAll(text=lambda text: isinstance(text, Comment))
[comment.extract() for comment in comments]
使用decompose()——方法将当前节点移除文档树并完全销毁:markup = 'I linked to example.com'
soup = BeautifulSoup(markup)
a_tag = soup.a
soup.i.decompose()
a_tag
# I linked to
中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/