我有这样的代码
@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