ruby Nokogiri xpath获取节点的内容

前端之家收集整理的这篇文章主要介绍了ruby Nokogiri xpath获取节点的内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的代码 @H_502_2@@doc = Nokogiri::HTML(open(url) @doc.xpath(query).each do |html| puts html # how get content of a node end

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

@H_502_2@<li class="stat">

解决方法

这是Nokogiri的 README file中的概要示例,显示了使用CSS,XPath或混合的方法: @H_502_2@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

猜你在找的Ruby相关文章