How to get text from 'td' tags from 'table' tag on html page using Mechanize

Go To


How to get texts from 'td' tags from 'table' on html page by using Mechanize gem?

2012-04-03 19:47
by megas
possible duplicate of Ruby Nokogiri Parsing HTML table IIthe Tin Man 2012-04-03 21:47
Also see: - the Tin Man 2012-04-03 21:50


I almost always use mechanize with nokogiri. This guide helped me get started.

Something like this should work (Untested):

require 'mechanize'
require 'nokogiri'

agent =
page = agent.get("")
doc = Nokogiri::HTML(page.body, "UTF-8")
doc.xpath('//td').each do |node|
  puts node.text

More information on nokogiri here

2012-04-03 21:28
by Dru
Thanks, Mechanize already uses nokogiri so it would be more correct to use xpath lookup from Mechaniz - megas 2012-04-03 21:41
Thanks, saved me a couple bits. In the past, I always required it separately - Dru 2012-04-03 21:58