1

此代码从谷歌图片中随机抓取一张图片。但是,当网络爬虫尝试搜索谷歌没有显示结果的术语时,我会收到错误。当谷歌向网络爬虫提供不再存在的图像时,我也会遇到错误。我如何编写此代码,以便在遇到错误时重新运行并尝试获取另一个图像。

require 'open-uri'
require 'nokogiri'
url = "https://www.google.com/search?hl=en&q=" + rand(0-999999).to_s + "&ion=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&bvm=bv.42553238,d.dmg&biw=1354&bih=622&um=1&ie=UTF-8&tbm=isch&source=og&sa=N&tab=wi&ei=sNEfUf-fHvLx0wG7uoG4DQ"
googim = Nokogiri::HTML(open(url))
googimstr = googim.to_s
durl = googim.to_s.split('imgurl=')[1].split('&amp')[0]

name = durl.reverse.split("/")[0].reverse

open("./data/images/#{name}", 'wb') do |file|
          file << open(durl).read
end

这是我收到的两种错误

第一个错误:

usr/lib/ruby/2.0.0/open-uri.rb:353:in `open_http': 400 Bad Request (OpenURI::HTTPError)
    from /usr/lib/ruby/2.0.0/open-uri.rb:708:in `buffer_open'
    from /usr/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
    from /usr/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
    from /usr/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
    from /usr/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
    from /usr/lib/ruby/2.0.0/open-uri.rb:688:in `open'
    from /usr/lib/ruby/2.0.0/open-uri.rb:34:in `open'
    from wc.rb:11:in `block in <main>'
    from /usr/lib/ruby/2.0.0/open-uri.rb:36:in `open'
    from /usr/lib/ruby/2.0.0/open-uri.rb:36:in `open'
    from wc.rb:10:in `<main>'

第二个错误:

wc.rb:6:in `split': invalid byte sequence in UTF-8 (ArgumentError)
    from wc.rb:6:in `<main>'
4

1 回答 1

2

您可以将代码的适当部分包装在一个begin/end块和rescue异常中。例如:

begin
  open("./data/images/#{name}", 'wb') do |file|
    file << open(durl).read
  end
rescue => e
  puts "some failure: #{e}"
end

这是 Pickaxe/Programming Ruby 的 Exceptions、Catch 和 Throw 章节的链接:http ://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

于 2013-03-28T21:34:29.410 回答