Radek Radek - 11 months ago 37
Ruby Question

How do I parse an HTML table with Nokogiri?

I installed Ruby and Mechanize. It seems to me that it is posible in Nokogiri to do what I want to do but I do not know how to do it.

What about this

? It is just part of the HTML of a vBulletin forum site. I tried to keep the HTML structure but delete some text and tag attributes. I want to get some details per thread like: Title, Author, Date, Time, Replies, and Views.

Please note that there are few tables in the HTML document? I am after one particular table with its
<tbody id="threadbits_forum_251">
. The name will be always the same (I hope). Can I use the
and the
in the code?

<table >
<tr> <!-- table header --> </tr>
<!-- show threads -->
<tbody id="threadbits_forum_251">
<a href="showthread.php?t=230708" >Vb4 Gold Released</a>
<span><a>Paul M</a></span>
06 Jan 2010 <span class="time">23:35</span><br />
by <a href="member.php?find=lastposter&amp;t=230708">shane943</a>
<td><a href="#">24</a></td>


Answer Source

require 'nokogiri'
require 'pp'

html = <<-EOS
  (The HTML from the question goes here)

doc = Nokogiri::HTML(html)
rows = doc.xpath('//table/tbody[@id="threadbits_forum_251"]/tr')
details = rows.collect do |row|
  detail = {}
    [:title, 'td[3]/div[1]/a/text()'],
    [:name, 'td[3]/div[2]/span/a/text()'],
    [:date, 'td[4]/text()'],
    [:time, 'td[4]/span/text()'],
    [:number, 'td[5]/a/text()'],
    [:views, 'td[6]/text()'],
  ].each do |name, xpath|
    detail[name] = row.at_xpath(xpath).to_s.strip
pp details

# => [{:time=>"23:35",
# =>   :title=>"Vb4 Gold Released",
# =>   :number=>"24",
# =>   :date=>"06 Jan 2010",
# =>   :views=>"1,320",
# =>   :name=>"Paul M"}]