【发布时间】:2011-03-27 15:26:02
【问题描述】:
假设我的 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等相关?
【问题讨论】: