2010-11-24 23 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 
相關問題