我正在实现一个需要抓取网站的工具。我使用银莲花来爬行,在每个银莲花的页面上,我使用boilerpipe和Nokogiri来管理HTML格式,等等。
我的问题是:如果我得到500内部服务器错误,它使Nokogiri失败,因为没有页面。
Anemone.crawl(name) do |anemone|
anemone.on_every_page do |page|
if not (page.nil? && page.not_found?)
result = Boilerpipe.extract(page.url, {:output => :htmlFragment, :extractor => :ArticleExtractor})
doc = Nokogiri::HTML.parse(result)
end
end
end在上面的例子中,如果有一个500内部服务器错误,应用程序将在Nokogiri::HTML.parse()上给出一个错误。我想避免这个问题。如果服务器给出一个错误,我想忽略这个页面继续计算。
有没有办法处理500内部服务器错误和404页找不到这些工具?
温馨的问候,雨果
发布于 2013-09-03 04:57:42
# gets the reponse of the link
res = Net::HTTP.get_response(URI.parse(url))
# if it returns a good code
if res.code.to_i >= 200 && res.code.to_i < 400 #good codes will be betweem 200 - 399
# do something with the url
else
# skip the object
next
end发布于 2013-09-03 04:56:04
我遇到了类似的问题。问题和答案在这里
https://stackoverflow.com/questions/18580441
复制相似问题