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 1060509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:20:16+00:00 2026-05-16T18:20:16+00:00

I need help parsing out some text from a page with lxml. I tried

  • 0

I need help parsing out some text from a page with lxml. I tried beautifulsoup and the html of the page I am parsing is so broken, it wouldn’t work. So I have moved on to lxml, but the docs are a little confusing and I was hoping someone here could help me.

Here is the page I am trying to parse, I need to get the text under the “Additional Info” section. Note, that I have a lot of pages on this site like this to parse and each pages html is not always exactly the same (might contain some extra empty “td” tags). Any suggestions as to how to get at that text would be very much appreciated.

Thanks for the help.

  • 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-16T18:20:16+00:00Added an answer on May 16, 2026 at 6:20 pm
    import lxml.html as lh
    import urllib2
    
    def text_tail(node):
        yield node.text
        yield node.tail
    
    url='http://bit.ly/bf1T12'
    doc=lh.parse(urllib2.urlopen(url))
    for elt in doc.iter('td'):
        text=elt.text_content()
        if text.startswith('Additional  Info'):
            blurb=[text for node in elt.itersiblings('td')
                   for subnode in node.iter()
                   for text in text_tail(subnode) if text and text!=u'\xa0']
            break
    print('\n'.join(blurb))
    

    yields

    For over 65 years, Carl Stirn’s Marine
    has been setting new standards of
    excellence and service for boating
    enjoyment. Because we offer quality
    merchandise, caring, conscientious,
    sales and service, we have been able
    to make our customers our good
    friends.

    Our 26,000 sq. ft. facility includes a
    complete parts and accessories
    department, full service department
    (Merc. Premier dealer with 2 full time
    Mercruiser Master Tech’s), and new,
    used, and brokerage sales.

    Edit: Here is an alternate solution based on Steven D. Majewski’s xpath which addresses the OP’s comment that the number of tags separating ‘Additional Info’ from the blurb can be unknown:

    import lxml.html as lh
    import urllib2
    
    url='http://bit.ly/bf1T12'
    doc=lh.parse(urllib2.urlopen(url))
    
    blurb=doc.xpath('//td[child::*[text()="Additional  Info"]]/following-sibling::td/text()')
    
    blurb=[text for text in blurb if text != u'\xa0']
    print('\n'.join(blurb))
    
    • 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.