【问题标题】:Using Nokogiri I am unable to find certain nodes in a document使用 Nokogiri 我无法在文档中找到某些节点
【发布时间】:2013-12-25 00:27:14
【问题描述】:

尝试在此页面上搜索 ysr-bio-data(“高度”值)http://sports.yahoo.com/footballrecruiting/football/recruiting/player-Jonathan-Allen-125805 的文档时

节点为零。这是因为 nokogiri 在填充此部分之前获取页面吗?还是 nokogiri 对象没有将整个页面存储到它的对象中? 下面是我如何尝试检索数据的一些示例代码。谢谢!

doc = Nokogiri::HTML(open('http://sports.yahoo.com/footballrecruiting/football/recruiting/player-Jonathan-Allen-125805'))

doc.css('ul#ysr-bio-data')

如果我需要提供任何其他信息,请告诉我。谢谢!

编辑:修正了不正确的语法。

【问题讨论】:

  • doc.css('ul#ysr-bio-data')
  • 只使用doc.css('#ysr-bio-data') 表单。
  • @majioa,我很抱歉,我在代码中使用了引号,只是没有正确复制。使用引号我仍然没有得到节点对象。谢谢!
  • 加引号但不加ul?
  • @majioa,是的,两者都试过了。我能够用我提供的答案解决它。信息是用ajax或其他东西提供的。所以我需要一种方法先将数据加载到内存中。

标签: ruby-on-rails ruby nokogiri


【解决方案1】:

抱歉,兄弟,但需要在页面上运行 javascript 才能填写这些单元格。

你可以这样做.. 让 javascript 在网络浏览器中运行..

require 'nokogiri' 
require 'watir-webdriver' #http://watir.com/

$browser = Watir::Browser.start "http://sports.yahoo.com/footballrecruiting/football/recruiting/player-Jonathan-Allen-125805"

doc = Nokogiri::HTML.parse($browser.html)

doc.css("ul#ysr-bio-data").text
=>  "Ht:6'3\"Wt:263 lbs40:4.5 secsBench Max:280Class:2013 (High School)\t"

我们基本上是用 watir 替换 open-uri。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我在 stackoverflow 上发现了另一个问题,它提供了解决我的问题的方法。

    HTML is read before fully loaded using open-uri and nokogiri

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-30
      • 2022-08-27
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多