【问题标题】:Using mechanize to check for div with similar but different names使用 mechanize 检查具有相似但不同名称的 div
【发布时间】:2012-12-05 01:31:20
【问题描述】:

目前我正在做以下事情:

if( firstTemp == true )
    total = doc.xpath("//div[@class='pricing  condense']").text
else
    total = doc.xpath("//div[@class='pricing  ']").text
end

我想知道我是否可以让 mechanize 自动获取包含字符串“pricing”的 div?

【问题讨论】:

标签: ruby xpath nokogiri mechanize


【解决方案1】:

docMechanize::Page 吗?通常约定是pagedoc 对于Nokogiri::HTML::Document。无论如何,尝试任何一种:

doc.search('div.pricing')

对于第一个,使用at 而不是search

doc.at('div.pricing')

【讨论】:

  • 它实际上是一个 Nokogiri::HTML::Document,从 Mechanize::Page 解析而来。感谢你的回答。 :0
  • 知道如何访问子节点吗?我曾经这样做过 doc.xpath("//div[@class='pricing ']/input") 但 doc.at('div.pricing')/input 似乎不起作用。
  • 是的,那就是:doc.at('div.pricing').at('input') 或只是 doc.at('div.pricing > input')
  • 好吧,这会更复杂。我可以喜欢匹配字符串与 'pricing' 但丢弃像 'pricing sold' 之类的字符串吗?
  • @user1043625 Mechanize::Page 已经将页面公开为 Nokogiri 文档。无需自己解析。确保您没有重复工作。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2014-05-03
相关资源
最近更新 更多