2011-01-31 70 views

回答

2
biggest_table = doc.xpath('//table').max_by do |table| 
    table.xpath('.//tr').length 
end 

或者,如果有一條領帶,也許你想用最行的所有表的列表:

# Hash mapping number of rows to array of table nodes 
tables = doc.xpath('//table').group_by{ |t| t.xpath('.//tr').length } 

# Array of all (possibly only 1) tables with the most rows 
biggest_n = tables[table.keys.max] 
+0

感謝Phrogz。我現在把它作爲一個單線程``largest_table = doc.xpath('// table')。max_by {| table | table.xpath('.// tr')。length}` – 2011-01-31 16:30:08

0

這可能不是您要查找的內容,但您可以在瀏覽器中使用jQuery或Prototype輕鬆完成此操作。

0
tables = @doc.xpath('//table') 
tr_count = tables.map{|n| n.xpath('tr|*/tr').length} 
biggest_table = tables[tr_count.index(tr_count.max)]