1

我正在尝试从 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 > 

谢谢!

4

1 回答 1

0

我能够使用当前版本的 Mechanize:2.7.0 使您的代码正常工作

于 2013-05-24T22:54:50.267 回答