且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从元素中删除所有属性

更新时间:2023-12-05 15:40:16

这是一种可能的方法,假设您要删除某些元素的 all 属性,例如table:

This is one possible way, assuming that you want to remove all attributes of certain element, say table :

for table in root.xpath('//table[@*]'):
    table.attrib.clear()

上面的代码循环遍历包含任何属性的所有table,然后调用elemet的attrib属性的clear()方法,因为该属性只是一个python字典.

The code above loop through all table that contains any attribute, then call clear() method of the elemet's attrib property, since the property is simply a python dictionary.