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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:31:17+00:00 2026-06-18T16:31:17+00:00

I need to extract the first table (not the material in the first table

  • 0

I need to extract the first table (not the material in the first table tag) from a section in a html. The table may spread out in multiple pages, so it may be under multiple table tags. There may be more than one table in the section. My logic is that if there are text node between table tags, then they are different tables. If there is no text node between tables tags, they are part of one table. How can I implement this?

I didn’t use xpath to find the first table because I need to identify the appropriate section first by using reg exp to check each text node.

html='<body>
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
<table border="1">
<tr>
<td>row 3, cell 1</td>   
<td>row 3, cell 2</td>
</tr>
<tr> 
<td>row 4, cell 1</td>
<td>row 4, cell 2</td>
</tr>
</table>
<p>text </p>                       # Split by text, the below is a different table
<table border="1">
<tr>
<td>row 5, cell 1</td>
<td>row 5, cell 2</td>
</tr>
<tr>
<td>row 6, cell 1</td>
<td>row 6, cell 2</td>
</tr>

</body>'

This is my current code, which only picks up the first table tag rather than first TABLE(row 1-4 in my sample). I used gem tabler parser for extract the table.

require 'nokogiri'
require 'table_parser'

doc = Nokogiri::HTML(html)
table = Array.new

i = 0
doc.traverse do |node|
    if node.name == 'table' && i == 0
        table = TableParser::Parser::extract_table(node, node.path)
        i +=1
    end
end

puts table
  • 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-06-18T16:31:18+00:00Added an answer on June 18, 2026 at 4:31 pm

    It sounds like you want to merge consecutive tables:

    # find each table that follows another table. Then reverse that so you're iterating from bottom to top.
    doc.search('table + table').to_a.reverse.each do |table|
      # add each of the tables tr's to the previous table
      table.search('tr').each{|tr| table.previous.add_child tr}
      # then remove the table
      table.remove
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract a value from a hidden HTML field, have somewhat figured
I need to extract records from a table, copy the data to a second
I need to create a new table from data I extract from the following
I want to extract data from HTML table using Python script and save it
I need to extract a table of data on a collection of pages. I
Need to extract .co.uk urls from a file with lots of entries, some .com
I need to extract the URL's from the php datas, how can i achieve
I need to extract the arguments from a function Say, Function_1(arg1,agr2,....argn). Is it possible
I need to extract numbers that are longer than 3 digits and do not
I need to extract images from a FB fan page. I'm able to read

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.