Q:

Nokogiri ruby: Iterate over table rows with no class name

I want to iterate over each row of a table.
This is the relevant source code showing 6 table rows in total.
3 of them have no class name and 3 others do, the ... represent some attributes.
<tbody>

    <tr> … </tr>
    <tr class="even"> … </tr>
    <tr> … </tr>
    <tr class="even"> … </tr>
    <tr> … </tr>
    <tr class="even"> … </tr>

</tbody>

Assuming that doc is a Nokogiri::HTML::Document the following code generates only 3 tr elements instead of 6. It only returns the tr elements having the class="even".
doc.css('#main_result table tbody tr').each do |tr|
  p tr
end

How can I now get an array of all tr elements, making it able to iterate over them?
This actual HTML can be found on the following link:
http://www.motogp.com/en/Results+Statistics/1949/TT/500cc/RAC
I don't really know how to paste the source code nicely... sorry

A:

The HTML in that page is malformed, and is missing some <tr> tags, it actually looks something like this:
<tbody>
    <td></td>
    ...
  </tr>
  <tr class="even">
    <td></td>
    ...
  </tr>
    <td></td>
    ...
  </tr>
  <tr class="even">
    <td></td>
    ...
  </tr>
    <td></td>
    ...
  </tr>
  <tr class="even">
    <td></td>
    ...
  </tr>
</tbody>

Note how only the tr tags with class="even" are present, the others are missing. Nokogiri therefore only sees three rows when parsing the page.
One possible solution to this could be to use Nokogumbo, which adds Google’s Gumbo HTML5 parser to Nokogiri, and better handles and corrects malformed HTML like this:
require 'nokogumbo' # install the gem first

doc = Nokogiri.HTML5(the_page)

puts doc.css('#main_result table tbody tr').size
# should now be 6 rather than 3

