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
end
end
which, for the first row, gives me
<span class="black">12:17AM </span>
<span class="black">
<a href="http://www.mta.info/mnr/html/planning/schedules/ref.htm"></a></span>
<span class="black">1:22AM </span>
<span class="black">
<a href="http://www.mta.info/mnr/html/planning/schedules/ref.htm"></a></span>
<span class="black">65</span>
<span class="black">TRANSFER AT STAMFORD (AR 1:01AM & LV 1:05AM) </span>
<span class="black">
N
</span>
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!)