使用user_agent_alias机械化抛出“坏参数(预期的URI对象或URI字符串)”

时间:2013-05-24 21:01:31

标签: ruby mechanize mechanize-ruby

我正在尝试从Twitter获取图片,但如果我使用user_agent_alias则会崩溃。我需要指定用户代理。但为什么使用代理别名呢?这是正常的吗?

使用和不使用{ |agent| agent.user_agent_alias = 'Mac Safari'}块尝试自己:

require "mechanize"
agent = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari'}
doc = agent.get("http://www.twitter.com")
doc.images

错误追踪:

ArgumentError: bad argument (expected URI object or URI string)
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/uri/generic.rb:1202:in `rescue in merge'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/uri/generic.rb:1199:in `merge'
    from /Users/luccasmaso/.rvm/gems/ruby-1.9.3-p392/gems/mechanize-2.6.0/lib/mechanize/page/image.rb:165:in `url'
    from /Users/luccasmaso/.rvm/gems/ruby-1.9.3-p392/gems/mechanize-2.6.0/lib/mechanize/page/image.rb:125:in `block in pretty_print'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:217:in `block (2 levels) in group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:243:in `nest'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:216:in `block in group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:228:in `group_sub'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:215:in `group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:164:in `object_group'
    from /Users/luccasmaso/.rvm/gems/ruby-1.9.3-p392/gems/mechanize-2.6.0/lib/mechanize/page/image.rb:124:in `pretty_print'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:154:in `block in pp'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:217:in `block (2 levels) in group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:243:in `nest'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:216:in `block in group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:228:in `group_sub'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/prettyprint.rb:215:in `group'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:154:in `pp'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:77:in `block in pp'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:121:in `guard_inspect_key'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:77:in `pp'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/pp.rb:51:in `pretty_inspect'
    from /Users/luccasmaso/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'1.9.3p392 :021 > 

谢谢!

1 个答案:

答案 0 :(得分:0)

我能够使用当前版本的Mechanize:2.7.0

来使代码正常工作