Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4567682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:57:58+00:00 2026-05-21T18:57:58+00:00

I’ve got an html file that has some text that looks like this (after

  • 0

I’ve got an html file that has some text that looks like this (after running it through lxml.html parse, lxml.html clean, and this is the result of etree.tostring(table, pretty_print=True))

 <tr><td>&#13;
224&#13;
9:00 am&#13;
-3:00 pm&#13;
NPHC Leadership</td>&#13;
<td>&#13;
<font>ALSO IN 223; WALL OPEN</font></td>&#13;

The documentation that I’ve found on lxml has been somewhat spotty. 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 <table>, <td>, and <tr>. 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 &#13;.

To strip the attributes currently I use:

    etree.strip_attributes(tree, 'width', 'href', 'style', 'onchange',
                           'ondblclick', 'class', 'colspan', 'cols',
                           'border', 'align', 'color', 'value',
                           'cellpadding', 'nowrap', 'selected',
                           'cellspacing')

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.

I tried using Cleaner, but when I passed it allow_tags, like this:

error: Cleaner(allow_tags=['table', 'td', 'tr']).clean_html(tree) it gave me this error:

ValueError: It does not make sense to pass in both allow_tags and remove_unknown_tags. Also, when I add remove_unkown_tags=False I get this error:

Traceback (most recent call last):
  File "parse.py", line 73, in <module>
    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

So, to sum up:

  1. I want to remove HTML entities, such as &#13;
  2. I want to remove all tags except <table>, <tr>, and <td>
  3. I want to remove all the attributes from the remaining tags.

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:57:59+00:00Added an answer on May 21, 2026 at 6:57 pm

    For me, I find writing it based on the basic elements of text, tag and tail makes it much easier to specialize the behaviour to what you want and include error checking (eg to ensure there are no unexpected tags in the incoming data).

    The if statements on the text and tail are because they return None rather than “” when zero length.

    def ctext(el):
        result = [ ]
        if el.text:
            result.append(el.text)
        for sel in el:
            if sel.tag in ["tr", "td", "table"]:
                result.append("<%s>" % sel.tag)
                result.append(ctext(sel))
                result.append("</%s>" % sel.tag)
            else:
                result.append(ctext(sel))
            if sel.tail:
                result.append(sel.tail)
        return "".join(result)
    
    html = """your input string"""
    el = lxml.html.fromstring(html)
    print ctext(el)
    

    Remember the relationship is:

      <b>text of the bold <i>text of the italic</i> tail of the italic</b>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.