user633230
user633230

Reputation: 21

How to get all text inside 'td' tags from 'table' tag on html page using Mechanize gem?

I am trying to parse table using Mechanize gem but i don't know how to iterate table.

Upvotes: 0

Views: 835

Answers (1)

Mladen Jablanović
Mladen Jablanović

Reputation: 44090

Mechanize uses nokogiri for parsing HTML, so you should look up the documentation there. Namely, take a look at xpath method.

Here's an example, parsing the current page:

require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://stackoverflow.com/questions/4265745/how-to-get-all-text-inside-td-tags-from-table-tag-on-html-page-using-mechaniz'))
table = doc.xpath('//table').first # getting the first table on the page
table.xpath('tr/td').count # getting all the td nodes right below table/tr and counting them
#=> 4

Upvotes: 2

Related Questions