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

Answers


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

Need Your Help

d3.js Day Hour Heatmap loads on server restart, but disappears when the page is refreshed

javascript angularjs twitter-bootstrap d3.js

I have a very weird situation on my hands and I'm not sure how to fix it. I will say that I am new to Angular.js and D3.js so if you see a newb mistake, it's probably not a typo, it's me having done

Save a webpage completely

html css website local

I have a small problem that concerns an outsourced website development company who will not (for perfectly normal and valid reason) allow us access to the server to alter stylesheets. I've been tas...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.