0
我想解析表使用机械化宝石,但我不知道如何迭代表。如何使用机械化宝石在html页面的'table'标签中获取'td'标签内的所有文本?
我想解析表使用机械化宝石,但我不知道如何迭代表。如何使用机械化宝石在html页面的'table'标签中获取'td'标签内的所有文本?
机械化使用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