ruby Nokogiri xpath获取节点的内容

Joh*_*ohn 8 ruby nokogiri nodes

我有这样的代码

@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|

  puts html # how get content of a node
end
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获取节点的内容,因为现在我得到了这样的东西.

<li class="stat">
Run Code Online (Sandbox Code Playgroud)

the*_*Man 12

这是Nokogiri 的README文件中的概要示例,显示了使用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
Run Code Online (Sandbox Code Playgroud)


Lee*_*vis 5

html.content 要么 html.text

请参阅节点文档