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/
最后
以上就是超级西牛最近收集整理的关于python删除html文本及子节点_利用BeautifulSoup删除/去除HTML指定标签和去除注释的全部内容,更多相关python删除html文本及子节点_利用BeautifulSoup删除/去除HTML指定标签和去除注释内容请搜索靠谱客的其他文章。
发表评论 取消回复