【发布时间】:2013-12-03 23:30:12
【问题描述】:
我正在使用 Mechanize 从 KBB.com 上抓取数据,我的应用获取一个 url,然后使用 Mechanize 浏览各个页面并找到给定 url 的价格。一个 url 可以指向许多不同的页面,我的应用程序需要能够确定它到达了哪个页面。我目前正在使用 if / else 语句来指导逻辑,但我遇到了一个我似乎无法调试的问题。
当我的应用到达 if / else 语句并检查条件时:
if agent.page.link_with(:text => "Choose this style")
我明白了:
NoMethodError (undefined method `link_with' for nil:NilClass)
我尝试使用以下方法进行调试:
debugger if agent.page.link_with(:text => "Choose this style").nil?
但这给了我与上面相同的错误,只是这一次它说调试器代码是问题的根源。
我该如何调试呢?或者有什么更好的编码方式,这样我的应用就不会遇到这个异常?
代码如下:
def kbb_value(url)
agent = Mechanize.new
# rescue bad urls
begin
page = agent.get(url)
rescue Mechanize::ResponseCodeError
begin
agent.cookie_jar.clear!
page = agent.get(url.sub('styles', 'categories'))
rescue Mechanize::ResponseCodeError
page = 'http://www.kbb.com/acura/rsx/2002-acura-rsx/styles/?intent=buy-used'
end
end
# kbb.com has cookie problems - solution is to clear cookies after every request
agent.cookie_jar.clear!
# program determines which starting page it is on via if / else
debugger if agent.page.link_with(:text => "Choose this style").nil?
if agent.page.link_with(:text => "Choose this style")
agent.page.link_with(:text => "Choose this style").click
# if the page allows additional options - skip that page
if agent.page.link_with(:text => "Choose price type")
agent.cookie_jar.clear!
agent.page.link_with(:text => "Choose price type").click
end
elsif agent.page.link_with(:text => "Choose price type")
agent.page.link_with(:text => "Choose price type").click
else
agent.page.link_with(:href => /bodystyle/).click
agent.cookie_jar.clear!
agent.page.link_with(:text => 'Choose this style').click
agent.cookie_jar.clear!
agent.page.link_with(:text => "Choose price type").click
end
# Get the 'Good' car price from kbb.com
agent.cookie_jar.clear!
agent.page.links_with(:text => "Get used car price")[2].click
# instead of getting the 'retail' value, substitute in 'private-party' in the url. Then get that page and grab the kbb value.
agent.cookie_jar.clear!
agent.get(agent.page.uri.to_s.sub('retail', 'private-party'))
@kbb_value = agent.page.at('.selected .value').text.delete('$')
end
【问题讨论】:
-
什么是agent.page?或者,更准确地说,你为什么将 page 设置为一个值然后从不使用它?
标签: ruby-on-rails debugging mechanize nomethoderror