I am trying to parse table using Mechanize gem but i don't know how to iterate tab开发者_JAVA百科le.
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
精彩评论