需要忽略基本的http身份验证

时间:2012-05-03 07:52:48

标签: ruby mechanize

我无法获取网页来源。

    require 'mechanize'

    agent = Mechanize.new        
    page = agent.get("https://#{ip}/")

结束有错误:

    /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:682:in `response_authenticate': 401 => Net::HTTPUnauthorized for https://82.144.208.6/cgi-bin/welcome.cgi -- no credentials found, provide some with #add_auth -- available realms: r722 (Mechanize::UnauthorizedError)
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:288:in `fetch'
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize.rb:407:in `get'
from /home/lord/ruby/ruby_backup/backup-done.ru:35:in `block (2 levels) in <main>'

如何忽略http auth,并获取源代码? THX

1 个答案:

答案 0 :(得分:1)

该例外包含一个页面访问器。文档描述了它:http://mechanize.rubyforge.org/Mechanize/ResponseCodeError.html

尝试:

begin
  page = agent.get ...
rescue Mechanize::ResponseCodeError => e
  page = e.page
end