加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby Nokogiri xpath获取节点的内容

发布时间:2020-12-17 04:26:36 所属栏目:百科 来源:网络整理
导读:我有这样的代码 @doc = Nokogiri::HTML(open(url)@doc.xpath(query).each do |html| puts html # how get content of a nodeend 我的问题是如何获得节点的内容,因为现在我得到了这样的东西. li class="stat" 解决方法 这是Nokogiri的 README file中的概要示
我有这样的代码
@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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读