2010-11-24 26 views

回答

2

机械化使用nokogiri解析HTML,所以你应该查看那里的文档。也就是说,看看xpath的方法。

下面是一个例子,分析当前页面:

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 
相关问题