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

  • SEARCH
  • Home
  • 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 3942432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:39:44+00:00 2026-05-20T00:39:44+00:00

I would like to parse a table using Nokogiri. I’m doing it this way

  • 0

I would like to parse a table using Nokogiri. I’m doing it this way

def parse_table_nokogiri(html)

    doc = Nokogiri::HTML(html)

    doc.search('table > tr').each do |row|
        row.search('td/font/text()').each do |col|
            p col.to_s
        end
    end

end

Some of the table that I have have rows like this:

<tr>
  <td>
     Some text
  </td>
</tr>

…and some have this.

<tr>
  <td>
     <font> Some text </font>
  </td>
</tr>

My XPath expression works for the second scenario but not the first. Is there an XPath expression that I could use that would give me the text from the innermost node of the cell so that I can handle both scenarios?


I’ve incorporated the changes into my snippet

def parse_table_nokogiri(html)

    doc = Nokogiri::HTML(html)
    table = doc.xpath('//table').max_by {|table| table.xpath('.//tr').length}

    rows = table.search('tr')[1..-1]
    rows.each do |row|

        cells = row.search('td//text()').collect {|text| CGI.unescapeHTML(text.to_s.strip)}
        cells.each do |col|

            puts col
            puts "_____________"

        end

    end

end
  • 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-20T00:39:45+00:00Added an answer on May 20, 2026 at 12:39 am

    Use:

    td//text()[normalize-space()]
    

    This selects all non-white-space-only text node descendents of any td child of the current node (the tr already selected in your code).

    Or if you want to select all text-node descendents, regardles whether they are white-space-only or not:

    td//text()
    

    UPDATE:

    The OP has signaled in a comment that he is getting an unwanted td with content just a '&#160;' (aka non-breaking space).

    To exclude also tds whose content is composed only of (one or more) nbsp characters, use:

    td//text()[translate(normalize-space(), '&#160;', '')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML/XML table generated by AJAX that displays limited columns of a
I'm inexperienced with sql in general, so using Hibernate is like looking for an
Should I be using RegularExpressions to do this? Possible to structure the results as
Thanks to Code Poet, I am now working off of this code to parse
I have a database table that has a SortOrder integer column. In the UI
How would you recommend I setup an event handler to fire when a variable
I'm still stuck on my problem of trying to parse articles from wikipedia. Actually
I wrote a RSS based application that will fetch some XML files, parse them
I have a word document in docx format with data in repeating format pattern.
Suppose you have a messaging system built in PHP with a MySQL database backend,

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.