我正在使用Nokogiri来抓取网页。几个网址需要被猜到,并且当它们不存在时返回404未找到错误。有没有办法捕获此异常?
http://yoursite/page/38475 #=> page number 38475 doesn't exist
我尝试了以下无效的方法。
url = "http://yoursite/page/38475"
doc = Nokogiri::HTML(open(url)) do
begin
rescue Exception => e
puts "Try again later"
end
end
答案 0 :(得分:21)
它不起作用,因为您没有挽救部分代码(它是open(url)
调用),在发现404状态时会引发错误。以下代码应该有效:
url = 'http://yoursite/page/38475'
begin
file = open(url)
doc = Nokogiri::HTML(file) do
# handle doc
end
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
# handle 404 error
else
raise e
end
end
BTW,关于救助Exception
:
Why is it a bad style to `rescue Exception => e` in Ruby?