def download_html(url)
begin
agent = Mechanize.new
agent.user_agent_alias = "Linux Mozilla"
# agent.open_timeout = 10
# puts agent.methods
html = agent.get(url)
rescue Exception=> e
puts "出现异常:"+e.message
puts e.backtrace
puts e.class
return "0"
end
agent.history.clear
return html
end
出现异常:execution expired
/home/liuxin/lib/ruby/2.3.0/net/http.rb:880:in `initialize'
/home/liuxin/lib/ruby/2.3.0/net/http.rb:880:in `open'
/home/liuxin/lib/ruby/2.3.0/net/http.rb:880:in `block in connect'
/home/liuxin/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
/home/liuxin/lib/ruby/2.3.0/net/http.rb:878:in `connect'
/home/liuxin/lib/ruby/2.3.0/net/http.rb:863:in `do_start'
/home/liuxin/lib/ruby/2.3.0/net/http.rb:858:in `start'
/home/liuxin/lib/ruby/gems/2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start'
/home/liuxin/lib/ruby/gems/2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:631:in `connection_for'
/home/liuxin/lib/ruby/gems/2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
/home/liuxin/lib/ruby/gems/2.3.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:274:in `fetch'
/home/liuxin/lib/ruby/gems/2.3.0/gems/mechanize-2.7.5/lib/mechanize.rb:464:in `get'
/media/liuxin/python/Ruby/RubymineProjects/Ruby_PC/class_all.rb:15:in `download_html'
/media/liuxin/python/Ruby/RubymineProjects/Ruby_PC/keji/kj.rb:31:in `jiexi'
/media/liuxin/python/Ruby/RubymineProjects/Ruby_PC/keji/kj.rb:87:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Net::OpenTimeout
最后发现是 IP 被限制了,但还有点别的问题。