ruby 使用Ruby从URL下载图像集合

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 使用Ruby从URL下载图像集合相关的知识,希望对你有一定的参考价值。

require 'open-uri'

def download_image(url, dest)
  open(url) do |u|
    File.open(dest, 'wb') { |f| f.write(u.read) }
  end
end

urls = [
  'http://petsfans.com/wp-content/uploads/2014/11/edfsaf.jpg',
  'http://dailynewsdig.com/wp-content/uploads/2012/06/funny-cats.jpg',
  'https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg'
]

urls.each { |url| download_image(url, url.split('/').last) }

以上是关于ruby 使用Ruby从URL下载图像集合的主要内容,如果未能解决你的问题,请参考以下文章