0

我在获取页面源时遇到了麻烦。

    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 并获取源代码?谢谢

4

1 回答 1

1

异常包含页面访问器。文档对其进行了描述: http: //mechanize.rubyforge.org/Mechanize/ResponseCodeError.html

尝试:

begin
  page = agent.get ...
rescue Mechanize::ResponseCodeError => e
  page = e.page
end
于 2012-05-03T09:22:43.870 回答