为什么我的rails if / else得到NoM​​ethodError

时间:2013-12-03 23:30:13

标签: ruby-on-rails debugging mechanize nomethoderror

我正在使用Mechanize从KBB.com中删除数据,我的应用程序需要一个网址,然后使用Mechanize浏览各个页面并找到给定网址的价格。网址可以导致许多不同的页面,我的应用程序需要能够找出它已登陆的页面。我目前正在使用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  

3 个答案:

答案 0 :(得分:2)

尝试使用:

agent.page.link_with(:text => "Choose this style") rescue debugger

似乎页面为零

答案 1 :(得分:1)

看起来它可能是两件事之一:

  • 我从未使用过机械,但您将结果分配给名为page的变量,而不是agent.page。尝试将其更改为与page.links_with进行比较,而不是agent.page.links_with,可能为空。

  • Ruby具有块变量作用域,并且块内定义的任何变量都不会存在于其之外。您正在开始内部分配您的页面(我相信,这是一个块范围)。尝试更改它,以便定义范围的页面外部(作为nil),然后只在begin/rescue中指定值。

代码修复示例:

# Assign the page variable outside of the scope
page = nil
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

# Check the "page" variable
debugger if page.link_with(:text => "Choose this style").nil?

注意:自从我使用了Ruby之后已经有一段时间了,因为我已经使用了Mechanize。给这些镜头,希望他们帮助。

答案 2 :(得分:0)

您收到此错误,因为Mechanize未加载页面。