如何删除整个文档中特定元素的所有属性。我正在尝试这样的事情:
from bs4 import UnicodeDammit
from lxml import html
content = open("source.html").read()
document = UnicodeDammit(content, is_html=True)
parser = html.HTMLParser(encoding=document.original_encoding)
root = html.document_fromstring(content, parser=parser)
for attr in root.xpath('.//table/@*'):
del attr.attrib
在这里,我试图使用xpath从文档中的所有表中删除所有属性,但这是行不通的。
假设您要删除某些元素的所有属性,这是一种可能的方法,例如table
:
for table in root.xpath('//table[@*]'):
table.attrib.clear()
上面的代码遍历table
包含任何属性的所有内容,然后调用clear()
elemetattrib
属性的方法,因为该属性只是一个python字典。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句