How do I download a picture using Ruby?

后端 未结 8 1467
滥情空心
滥情空心 2020-12-02 16:08

I want to download this picture using Ruby. How do I do that?

http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg

I am using Mac O

相关标签:
8条回答
  • 2020-12-02 16:43

    If you want to download all the images on one page, you can use the image_downloader gem:

    require 'rubygems'
    require 'image_downloader'
    
    downloader = ImageDownloader::Process.new('www.test.com','img_dir/')
    
    downloader.parse(:any_looks_like_image => true)
    
    downloader.download()
    
    0 讨论(0)
  • 2020-12-02 16:43

    To make things cleaner I would suggest you use the File's name itself when you save the image. I had an issue saving bulk images because they were not formated correctly. The images were saved like this:

    #pic.jpg(1)
    #pic.jpg(2)
    #etc.
    

    Thats why you should probably use the image name as the file name like this:

    src = 'http://www.asite.com/pic.jpg'    
    agent.get(src).save "#{folder}/#{File.basename(src)}"
    

    File.basename takes the image url and just returns the the actual image name:

    File.basename("http://www.asite.com/pic.jpg")
    # returns the image name
    pic.jpg
    
    0 讨论(0)
提交回复
热议问题