开发者

How to get all text inside 'td' tags from 'table' tag on html page using Mechanize gem?

I am trying to parse table using Mechanize gem but i don't know how to iterate tab开发者_JAVA百科le.


Mechanize uses nokogiri for parsing HTML, so you should look up the documentation there. Namely, take a look at xpath method.

Here's an example, parsing the current page:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜