Skip to content

Instantly share code, notes, and snippets.

@streetcarmonkey
Created May 30, 2015 08:17
Show Gist options
  • Save streetcarmonkey/967b50b50cb59a26a212 to your computer and use it in GitHub Desktop.
Save streetcarmonkey/967b50b50cb59a26a212 to your computer and use it in GitHub Desktop.
Ruby Nokogiri: Get class attribute of a TR element (or any other node for that matter)
require 'nokogiri'
html = %q{
<table>
<tr class="even_class">
<td></td>
</tr>
<tr class="odd_class">
<td></td>
</tr>
</table>
}
doc = Nokogiri::HTML(html)
rows = doc.xpath("//tr")
rows.each do | row |
puts row.attr('class')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment