我试图在网站上搜索图片,然后将其下载到本地文件夹。
我使用看起来很简单的剪贴画宝石。
我已经安装了它,制作了一个scrape.rb并从终端ruby scrape.rb
运行它
require 'scrapifier'
'http://www.blog.com'.scrapify(images: :jpg)
这将返回带有信息的哈希,但我想将该信息中的图像下载到本地文件夹。有任何想法吗?谢谢!
答案 0 :(得分:0)
你可以做这样的事情
require 'scrapifier'
require 'httparty'
#This will iterate on the array of images and download them
def extract_images(arr)
arr.each do |image_url|
file_name = File.basename(image_url)
File.open(file_name, "wb") do |f|
f.write HTTParty.get(image_url).parsed_response
end
end
end
a = 'http://www.amazon.com'.scrapify(images: :jpg)
extract_images(a[:images])
以上会将照片下载到当前执行程序
的文件夹中