John
John

Reputation: 83

Parsing a table with Nokogiri

I feel like I must be so close on this one but I cant seem to identify the right table name to get the data from. Could someone take a quick look at this and tell me where I am going wrong? Pretty sure I am currently getting no data back from the table variable?

def oddsmath

   require 'nokogiri'


   tables_data = []

   doc = Nokogiri::HTML(open("http://www.oddsmath.com/odds-comparison/"))

  doc.css('table#table-odds-comparison.tbl-odds-comparison.tablesorter.tablesorter-default.hasStickyHeaders').each do |table|

# find all rows in the current table, then iterate over the second all the way to the final one...
table.css('tr')[1..-1].each do |tr|

# collect the cell data and raw names from the remaining rows' cells...
raw_name = tr.at('th').text
cell_data = tr.css('td').map(&:text)

# aggregate it...
tables_data += [raw_name, cell_data]
end
end
@output=tables_data

end

Upvotes: 0

Views: 188

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

If you look at the actual HTML returned when you visit that page, you'll see the table is actually empty, and the contents are dynamically loaded by JS. Because of that you can't do what you want with just nokogiri opening the page. You'll need to use something that allows you to control a real browser (or emulates a browser with JS support) in order for the page to fully load before getting the page contents, or you'll need to figure out what URL the page is loading the data for the table from and see if you can access it directly from there (may not be possible).

Upvotes: 1

Related Questions