0
Mechanize gemを使用してテーブルを解析しようとしていますが、テーブルを反復する方法がわかりません。Mechanize gemを使ってhtmlページの 'table'タグから 'td'タグ内のすべてのテキストを取得するには?
Mechanize gemを使用してテーブルを解析しようとしていますが、テーブルを反復する方法がわかりません。Mechanize gemを使ってhtmlページの 'table'タグから 'td'タグ内のすべてのテキストを取得するには?
Mechanizeは、HTMLを解析するためにnokogiri
を使用しています。そのため、そこでドキュメントを調べる必要があります。すなわち、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
:ここ
は、現在のページを解析し、例を示します