我是靠谱客的博主 超级西牛,这篇文章主要介绍python删除html文本及子节点_利用BeautifulSoup删除/去除HTML指定标签和去除注释,现在分享给大家,希望可以做个参考。

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指定标签和去除注释内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部