开发者

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

开发者 https://www.devze.com 2023-01-27 05:41 出处:网络
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, ta

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

精彩评论

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