ruby - How to handle 404 not found errors in Nokogiri -


i using nokogiri scrape web pages. few urls need guessed , returns 404 not found error when don't exist. there way capture exception?

http://yoursite/page/38475 #=> page number 38475 doesn't exist 

i tried following didn't work.

url = "http://yoursite/page/38475" doc = nokogiri::html(open(url))   begin     rescue exception => e       puts "try again later"   end end 

it doesn't work, because not rescuing part of code (it's open(url) call) raises error in case of finding 404 status. following code should work:

url = 'http://yoursite/page/38475' begin   file = open(url)   doc = nokogiri::html(file)     # handle doc   end rescue openuri::httperror => e   if e.message == '404 not found'     # handle 404 error   else     raise e   end end 

btw, rescuing exception: why bad style `rescue exception => e` in ruby?


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -