9
比方說,我的HTML文件是這樣的:如何獲得Nokogiri中的下一個HTML元素?
<div class="headline">News</div>
<p>Some interesting news here</p>
<div class="headline">Sports</div>
<p>Baseball is fun!</p>
我可以得到headline
的div用下面的代碼:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "mypage.html"
doc = Nokogiri::HTML(open(url))
doc.css(".headline").each do |item|
puts item.text
end
但是我怎麼訪問內容在以下p
標記,以便News
與Some interesting news here
等有關?
當我使用'next_element'時,我得到一個'nil'對象**編輯**它看起來像''標題「div'是在'span'裏面。我將更新XML示例 – cbmeeks 2011-03-22 17:00:08
這沒什麼用。我必須做'item.parent.next_element',因爲標題div在span標籤內。謝謝! – cbmeeks 2011-03-22 17:06:57
然後您可能需要驗證該HTML。如果我記得正確的div不應該在span標籤內。 – 2011-03-27 07:28:12