问题
@p = mechanize.get(url)
tables = @p.search('table.someclass')
I'm basically going over about 200 pages, putting the tables in an array and the only way to sort is to find the table with the greatest number of rows.
So I want to be able to look at each item in the array and select the first item with the greatest number of rows.
I've been trying to use max_by
but that won't work because I'm needing to search the table that is the array item, to find the tr.count.
回答1:
Two ways:
biggest = tables.max_by{ |table| table.css('tr').length }
biggest = tables.max_by{ |table| table.xpath('.//tr').length }
Since you didn't give an example URL, here's a similar search showing that max_by
can be used:
require 'mechanize'
mechanize = Mechanize.new
rows = mechanize.get("http://phrogz.net/").search('table#infoporn tbody tr')
# Find the table row from the array that has the longest link URL in it
biggest = rows.max_by{ |tr| tr.at_xpath('.//a/@href').text.length }
p biggest.name, biggest.at('.//a/@href')
#=> "tr"
#=> [#<Nokogiri::XML::Attr:0x1681680 name="href" value="slow-file-reads-on-windows-ruby-1.9">]
来源:https://stackoverflow.com/questions/8233566/find-table-in-an-array-with-the-most-rows-using-ruby-nokogiri-and-mechanize