From: Alex Young Date: 2006-08-09T05:21:55+09:00 Subject: Re: mechanize timeout errors akanksha wrote: > could you plz elaborate a little on that or point me to an example. > > > Alex Young wrote: > >>Berger, Daniel wrote: >> >>>>-----Original Message----- >>>>From: akanksha [mailto:akanksha.baid@gmail.com] >>>>Sent: Tuesday, August 08, 2006 12:45 PM >>>>To: ruby-talk ML >>>>Subject: mechanize timeout errors >>>> >>>> >>>>I am using mechanize for scraping some urls. >>>>begin >>>> page = agent.get(url) >>>>rescue >>>> puts "oops!!" >>>>end >>>> >>>>catches invalid urls etc. , but how to I handle timeout >>>>errors? In particular this is the error I get : >>>> >>>>request-header: accept => */* >>>>request-header: user-agent => WWW-Mechanize/0.5.1 >>>>(http://rubyforge.org/projects/mechanize/) >>>>/usr/local/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': >>>>execution expired >>>>(Timeout::Error) >>>> from /usr/local/lib/ruby/1.8/timeout.rb:56:in `timeout' >>>> from /usr/local/lib/ruby/1.8/timeout.rb:76:in `timeout' >>> >>> >>>begin >>> page = agent.get(url) >>>rescue Timeout::Error >>> puts "Timeout!" >>> raise >>>rescue >>> puts "Some other error!" >>> raise >>>end >>> >>>If you want control over the timeout value I think you'll need to >>>re-wrap the call to agent.get in your own timeout block: >> >>Not so: WWW::Mechanize#read_timeout= is your friend. >> >>-- >>A;ex > > > Sure: irb(main):001:0> require 'mechanize' => true irb(main):002:0> agent = WWW::Mechanize.new; irb(main):003:0> agent.read_timeout = 0.1 # set a 0.1sec timeout => 0.1 irb(main):004:0> begin irb(main):005:1* agent.get("http://www.ruby-doc.org") irb(main):006:1> rescue Timeout::Error irb(main):007:1> puts "Timeout!" irb(main):008:1> end Timeout! => nil irb(main):009:0> -- Alex