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

Go To StackoverFlow.com

0

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: http://stackoverflow.com/a/8233949/12842 - the Tin Man 2012-04-03 21:50


2

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

Something like this should work (Untested):

require 'mechanize'
require 'nokogiri'

agent = Mechanize.new
page = agent.get("http://www.google.com/")
doc = Nokogiri::HTML(page.body, "UTF-8")
doc.xpath('//td').each do |node|
  puts node.text
end

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