Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove certain attributes from HTML tags

How can I remove certain attributes such as id, style, class, etc. from HTML code?

I thought I could use the lxml.html.clean module, but as it turned out I can only remove style attributes with Clean(style=True).clean_html(code). I'd prefer not to use regular expressions for this task (attributes could change).

What I would like to have:

from lxml.html.clean import Cleaner

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

cleaner = Cleaner(style=True, id=True, class=True)
cleaned = cleaner.clean_html(code)

print cleaned
'<tr>'

Thanks in advance!

like image 724
naeg Avatar asked Sep 19 '11 11:09

naeg


1 Answers

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>
like image 160
unutbu Avatar answered Sep 29 '22 10:09

unutbu