Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I remove html entities (and more) using lxml?
    primarykey
    data
    text
    <p>I've got an html file that has some text that looks like this (after running it through <code>lxml.html parse</code>, <code>lxml.html clean</code>, and this is the result of <code>etree.tostring(table, pretty_print=True)</code>)</p> <pre><code> &lt;tr&gt;&lt;td&gt;&amp;#13; 224&amp;#13; 9:00 am&amp;#13; -3:00 pm&amp;#13; NPHC Leadership&lt;/td&gt;&amp;#13; &lt;td&gt;&amp;#13; &lt;font&gt;ALSO IN 223; WALL OPEN&lt;/font&gt;&lt;/td&gt;&amp;#13; </code></pre> <p>The documentation that I've found on lxml has been somewhat <a href="http://lxml.de/lxmlhtml.html">spotty</a>. I've been able to do quite a bit to get to this point, but what I would like to do is strip out all the tags except <code>&lt;table&gt;</code>, <code>&lt;td&gt;</code>, and <code>&lt;tr&gt;</code>. I would also like to strip all the attributes from those tags, and I would also like to get rid of the entities, such as <code>&amp;#13;</code>.</p> <p>To strip the attributes currently I use:</p> <pre><code> etree.strip_attributes(tree, 'width', 'href', 'style', 'onchange', 'ondblclick', 'class', 'colspan', 'cols', 'border', 'align', 'color', 'value', 'cellpadding', 'nowrap', 'selected', 'cellspacing') </code></pre> <p>which works fine, but it seems like there should be a better way. It seems like there should be some fairly simple methods to do what I want, but I haven't been able to find any examples that worked right for me. </p> <p>I tried using <code>Cleaner</code>, but when I passed it <code>allow_tags</code>, like this:</p> <p>error: <code>Cleaner(allow_tags=['table', 'td', 'tr']).clean_html(tree)</code> it gave me this error:</p> <p><code>ValueError: It does not make sense to pass in both allow_tags and remove_unknown_tags</code>. Also, when I add <code>remove_unkown_tags=False</code> I get this error:</p> <pre><code>Traceback (most recent call last): File "parse.py", line 73, in &lt;module&gt; SParser('schedule.html').test() File "parse.py", line 38, in __init__ self.clean() File "parse.py", line 42, in clean Cleaner(allow_tags=['table', 'td', 'tr'], remove_unknown_tags=False).clean_html(tree) File "/usr/lib/python2.6/dist-packages/lxml/html/clean.py", line 488, in clean_html self(doc) File "/usr/lib/python2.6/dist-packages/lxml/html/clean.py", line 390, in __call__ el.drop_tag() File "/usr/lib/python2.6/dist-packages/lxml/html/__init__.py", line 191, in drop_tag assert parent is not None AssertionError </code></pre> <p>So, to sum up:</p> <ol> <li>I want to remove HTML entities, such as <code>&amp;#13;</code> </li> <li>I want to remove all tags except <code>&lt;table&gt;</code>, <code>&lt;tr&gt;</code>, and <code>&lt;td&gt;</code></li> <li>I want to remove all the attributes from the remaining tags.</li> </ol> <p>Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload