remove certain attributes from HTML tags

隐身守侯 提交于 2019-12-03 14:26:38

cleaner.Cleaner.__call__ has a safe_attrs_only parameter. When set to True, only attributes in clean.defs.safe_attrs are preserved. You can remove any or all attributes by changing clean.defs.safe_attrs. Just be sure to change it back when you are done.

import lxml.html.clean as clean

code = '<tr id="ctl00_Content_AdManagementPreview_DetailView_divNova" class="Extended" style="display: none;">'

safe_attrs = clean.defs.safe_attrs
cleaner = clean.Cleaner(safe_attrs_only=True, safe_attrs=frozenset())
cleansed = cleaner.clean_html(code)

print(cleansed)

yields

<tr></tr>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!