如何通过将所有其他标签完整地保存在Python中来删除脚本标签及其内容

发布于 2025-01-29 12:11:21 字数 465 浏览 3 评论 0原文

我试图在Python中消毒HTML内容。我尝试使用lxml.html.Clean如下下面

from lxml.html.clean import Cleaner
from lxml import html

cleaner = Cleaner(page_structure=False, safe_attrs=html.defs.safe_attrs | set(['style']), scripts=True)
cleaned_html_content = cleaner.clean_html(html_content),

删除,但是某些属性也被删除了,例如< input type =“复选框”已选中的“检查”已检查> is也被删除。我可以使用< script>带有lxml.html.clean的标签?

I am trying to sanitize HTML content in python. I tried using lxml.html.clean as below

from lxml.html.clean import Cleaner
from lxml import html

cleaner = Cleaner(page_structure=False, safe_attrs=html.defs.safe_attrs | set(['style']), scripts=True)
cleaned_html_content = cleaner.clean_html(html_content),

The tag is getting removed, but some attributes are also getting removed like <input type="checkbox" checked disabled> is also getting removed. Can I remove only the <script> tags with lxml.html.clean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文