2010-11-24 34 views

Répondre

2

Mechanize utilise nokogiri pour analyser le code HTML, vous devriez donc consulter la documentation. À savoir, jetez un oeil à la méthode xpath.

Voici un exemple, l'analyse de la page en cours:

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