Find table in an array with the most rows using Ruby, Nokogiri and Mechanize

假装没事ソ 提交于 2019-12-08 08:39:44

问题


@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!