为什么这个 Nokogiri XPath 有一个空返回? [英] Why does this Nokogiri XPath have a null return?
问题描述
我正在使用 NOKOGIRI 浏览网页.我熟悉 XPath,但我不明白为什么我的 XPath 无法选择特定行.查看 ruby 代码.
I'm XPath-ing through a web page with NOKOGIRI. I'm familiar with XPath, but I cannot figure out why my XPath fails to pick up the specific row. See the ruby code.
我使用 FireBug XML 来验证我的 XPath,所以我 99% 确定我的 XPath 是正确的.
I used FireBug XML to validate my XPath, so I am 99% sure my XPath is correct.
require 'nokogiri'
require 'open-uri'
@searchURL = 'http://www.umn.edu/lookup?UID=smit4562'
@xpath = '//html/body/p/table/tbody/tr/td[2]/table/tbody/tr[2]'
doc = Nokogiri::HTML(open(@searchURL))
puts 'row should be = Email Address: smit4562@umn.edu'
puts '=> ' + doc.xpath(@xpath).to_s
puts 'is row emppty?'
puts '=> ' + doc.xpath(@xpath).empty?().to_s
推荐答案
The Firebug 使用生成的 DOM,其中包含 Firebug uses the generated DOM, which does contains the 删除两个 Remove both the 这篇关于为什么这个 Nokogiri XPath 有一个空返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<tbody>
标签是一个可选标签,如果省略则是隐式的.这意味着 标签在不存在时由浏览器自动插入.它们不在您的示例中的源代码中,因此 nokogiri 不知道它们.
<tbody>
tag is an optional tag which is implicit if it is omitted. This means the <tbody>
tags are inserted automatically by the browser when not present. They are not in the source code in your example, so nokogiri doesn't know about them.tbody
元素,因此该语句在浏览器中确实匹配.tbody
elements, so the statement does match inside a browser.tbody
选择器,你应该没问题.tbody
selectors and you should be fine.
登录
关闭