Ruby 是否从Scrapier下载以哈希形式返回的图像?
我试图在一个网站上抓取图像,并将其下载到本地文件夹 我用的是看起来很简单的刮刀 我已经安装了它,制作了一个scrape.rb,并从终端ruby scrape.rb运行了它Ruby 是否从Scrapier下载以哈希形式返回的图像?,ruby,scrape,Ruby,Scrape,我试图在一个网站上抓取图像,并将其下载到本地文件夹 我用的是看起来很简单的刮刀 我已经安装了它,制作了一个scrape.rb,并从终端ruby scrape.rb运行了它 这将返回一个包含信息的散列,但我想将该信息中的图像下载到本地文件夹。有什么想法吗?谢谢 你可以这样做 require 'scrapifier' require 'httparty' #This will iterate on the array of images and download them def extract_
这将返回一个包含信息的散列,但我想将该信息中的图像下载到本地文件夹。有什么想法吗?谢谢 你可以这样做
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])
上面的内容将把照片下载到您当前的程序执行文件夹中在这种情况下,定义方法应该在执行之前。@我和norokiri合作了,但你的解决方案也很有效。我在想,我能得到图像的尺寸吗?也许是和Rmagick?我想只下载图像,是大于500px也,这将刮只有一个网址。我想该计划,以刮在网站的所有路径的图像@吉布森,一切都可以,我正在迭代图像数组并下载。我在本地进行了测试,它下载了从amazon.com返回的所有图像
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])