How to remove all attributes from element

后端 未结 1 1441
感情败类
感情败类 2021-01-18 16:54

How to remove all attributes of the specific elements througout the document. I\'m trying something like this:

from bs4 import UnicodeDammit
from lxml import         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 17:47

    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()
    

    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.

    0 讨论(0)
提交回复
热议问题