【发布时间】:2011-07-19 14:15:45
【问题描述】:
我有这样的代码:
@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|
puts html # how get content of a node
end
如何获取节点的内容而不是这样的:
<li class="stat">
【问题讨论】:
我有这样的代码:
@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|
puts html # how get content of a node
end
如何获取节点的内容而不是这样的:
<li class="stat">
【问题讨论】:
这是 Nokogiri 的 README file 中的概要示例,展示了使用 CSS、XPath 或混合实现它的一种方法:
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
####
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
puts link.content
end
####
# Search for nodes by xpath
doc.xpath('//h3/a[@class="l"]').each do |link|
puts link.content
end
####
# Or mix and match.
doc.search('h3.r a.l', '//h3/a[@class="l"]').each do |link|
puts link.content
end
【讨论】:
请参阅 html.content 或 html.text。
请参阅Node documentation 了解更多信息。
【讨论】: