Help us understand the problem. What is going on with this article?

RubyでCSVのURLから画像をダウンロードする

More than 3 years have passed since last update.

Ruby標準ライブラリCSVを使用

リファレンス
要約

CSV (Comma Separated Values) を扱うライブラリです。

投入用のサンプルCSV

data.csv
id,url
1,http/example.com/foo.png
2,http/example.com/bar.png
3,http/example.com/foobar.png

CSVファイルと同じディレクトリにsave_image.rbを配置して実行

save_image.rb
require 'CSV'
require 'open-uri'

def save_image
  table = CSV.table('data.csv')
  save_dir = 'path/to/images/'

  # create directory,if not exist.
  FileUtils.mkdir_p(save_dir) unless FileTest.exist?(save_dir)

  table.each do |row|
    @url = row.field(:url)
    file_name = File.basename(@url).chomp
    file_path = save_dir + file_name

    open(file_path,'wb') do |output|
      open(@url) do |data|
        output.write(data.read)
      end
    end
  end
end

保存先に指定したディレクトリに画像がダウンロードされてたら完了です
CSVライブラリ最高です

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away