Remove class attribute from HTML using Python and lxml
I can't test this at the moment but this appears to be the general idea
for tag in node.xpath('//*[@class]'):
tag.attrib.pop('class')
lxml.html.clean.Cleaner does work, but needs proper configuration.
import lxml.html
from lxml.html import clean
html_string = '<p id="test" class="DumbClass">Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>'
tree = html.fromstring(html_string)
cleaner = html.clean.Cleaner()
cleaner.safe_attrs_only = True
cleaner.safe_attrs=frozenset(['id'])
cleaned = cleaner.clean_html(tree)
print(html.tostring(cleaned))
Result in :
b'<p id="test">Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>'