Skip to main content
 首页 » 编程设计

python之删除python中的所有html

2025年05月04日9JeffreyZhao

有没有一种方法可以使用 lxml.html 而不是具有一些 xss 问题的 beautifulsoup 来删除/转义 html 标签?我尝试使用清洁器,但我想删除所有 html。

请您参考如下方法:

尝试在元素上使用 .text_content() 方法,最好是在使用 lxml.html.clean 去除不需要的内容(脚本标签等... ).例如:

from lxml import html 
from lxml.html.clean import clean_html 
 
tree = html.parse('http://www.example.com') 
tree = clean_html(tree) 
 
text = tree.getroot().text_content()