您可以使用XPath執行幾個步驟。首先,你需要找到的table
的「水平」(即如何嵌套它是在其他表),然後找到所有後代tr
有相同數量的table
祖先:
tables = doc.xpath('//table')
tables.each do |table|
level = table.xpath('count(ancestor-or-self::table)')
rows = table.xpath(".//tr[count(ancestor::table) = #{level}]")
# do what you want with rows...
end
在更一般的情況下,在這裏你可能tr
嵌套直接其它tr
S,你可以做這樣的事情(這將是無效的HTML,但你可能有XML或其他一些標籤):
tables.each do |table|
# Find the first descendant tr, and determine its level. This
# will be a "top-level" tr for this table. "level" here means how
# many tr elements (including itself) are between it and the
# document root.
level = table.xpath("count(descendant::tr[1]/ancestor-or-self::tr)")
# Now find all descendant trs that have that same level. Since
# the table itself is at a fixed level, this means all these nodes
# will be "top-level" rows for this table.
rows = table.xpath(".//tr[count(ancestor-or-self::tr) = #{level}]")
# handle rows...
end
第一步可以分爲兩個單獨的查詢,可能更清楚:
first_tr = table.at_xpath(".//tr")
level = first_tr.xpath("count(ancestor-or-self::tr)")
(如果有表無tr
小號雖然,這將失敗,因爲first_tr
將nil
。上面的組合XPath可以正確處理這種情況。)
Nokogiri實現了CSS,其中包括一些jQuery擴展,所以熟悉樣式表選擇器的工作方式,並且應該有更好的運氣。 CSS更具可讀性,但XPath更強大,因此瞭解這兩方面都很好。在生成的HTML中很少使用'tbody'標記,但是當您查看頁面HTML時,瀏覽器往往會將它們粘在一起。不要相信瀏覽器,而是直接在命令行中使用'wget'或'curl'或'nokogiri'查看HTML。如果原始HTML包含它,只能使用'tbody'。 –
@Dave:只是好奇:爲什麼你會接受一個答案,而不是upvote呢? –