Tom Lehman Tom Lehman - 5 months ago 30
Ruby Question

Best way to parse a table in Ruby

I'd like to parse a simple table into a Ruby data structure. The table looks like this:

alt text

Edit: Here is the HTML

and I'd like to parse it into an array of hashes. E.g.,:

schedule[0]['NEW HAVEN'] == '4:12AM'
schedule[0]['Travel Time In Minutes'] == '95'

Any thoughts on how to do this? Perl has HTML::TableExtract, which I think would do the job, but I can't find any similar library for Ruby.


You might like to try Hpricot (gem install hpricot, prepend the usual sudo for *nix systems)

I placed your HTML into input.html, then ran this:

require 'hpricot'

doc = Hpricot.XML(open('input.html'))

table = doc/:table

(table/:tr).each do |row|
  (row/:td).each do |cell|
    puts cell.inner_html

which, for the first row, gives me

<span class="black">12:17AM </span>
<span class="black">
    <a href=""></a></span>
<span class="black">1:22AM  </span>
<span class="black">
    <a href=""></a></span>
<span class="black">65</span>
<span class="black">TRANSFER AT STAMFORD (AR 1:01AM & LV 1:05AM)                                                                            </span>
<span class="black">



So already we're down to the content of the TD tags. A little more work and you're about there.

(BTW, the HTML looks a little malformed: you have <th> tags in <tbody>, which seems a bit perverse: <tbody> is fairly pointless if it's just going to be another level within <table>. It makes much more sense if your <tr><th>...</th></tr> stuff is in a separate <thead> section within the table. But it may not be "your" HTML, of course!)