ruby - XPATH 索引不同?

标签 ruby xpath nokogiri mechanize

我在 Ruby 上使用 Mechanize,并在我的 Ruby 控制台中注意到以下内容:

1.9.3p194 :188 > testpage = Mechanize.new.get "http://www.sis.umd.edu/bin/soc?term=201208&crs=AGNR"
...
1.9.3p194 :184 > testpage.search("font[@face='arial,helvetica']").last.search("b[2]").text
 => "" 
1.9.3p194 :185 > testpage.search("font[@face='arial,helvetica']").last.search("b")[1].text
 => "Special Problems:\nSpecial Problems;"

为什么访问第二个b XPATH 中的元素给出的结果与访问所有 Nokogiri b 的第二个元素不同元素返回给出不同的结果?

最佳答案

search("b[2]")表示任何 b那是第二个 b相对于其
父元素。它将是一个 NodeSet,它可能有多个元素。
search("b")[1]表示第二个 b它发现。它将是一个节点或零。

关于ruby - XPATH 索引不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11695729/

相关文章:

mysql - 对大型表中的所有记录执行批量更新的最佳方法是什么

ruby-on-rails - ActiveRecord:find_by 忽略自定义 getter

html - 包含具有特定文本的另一个元素的元素的 XPath?

ruby - 使用 Nokogiri 读取 XML 时出现问题

ruby-on-rails - 使用 Devise/Rails 获取当前用户的 UserID

ruby-on-rails - rake 测试 :prepare fails with Rails 4. 0.8

Python + XPath : Is it possible to select the next element after the one I actually want?

php - 使用 XPath 获取最高值节点

ruby - Nokogiri 保持 HTML 实体不变

html - nokogiri:xml 到 html