我想要下载此链接中提供的产品的所有图像:
http://www.veromoda.in/vero-moda-women-s-white-coloured-casual-shirt-4.html
我知道如何通过传递每个图像的URL来下载单个图像:
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
link = 'http://www.veromoda.in/media/catalog/product/cache/1/thumbnail/9df78eab33525d08d6e5fb8d27136e95/1/0/10074424-snowwhite-1.jpg'
agent.get(link).save "images/pic.jpg"
如何通过传递一个url来下载该产品的所有图像?
发布于 2016-01-07 02:09:11
这是在页面中查找所有图像的核心:
require 'nokogiri'
require 'restclient'
doc = Nokogiri::HTML(RestClient.get('http://www.iana.org/domains/reserved'))
images = doc.search('img').map{ |img| img['src'] }
images[0..2] # => ["/_img/2013.1/iana-logo-header.svg", "/_img/2013.1/icann-logo.svg"]
循环遍历images
中的src
URL,对每个URL执行get
,然后保存它。
您可以看到,需要对URL进行一些修复,以使其完全定义,因此这是下载文件任务的一部分。您需要弄清楚如何做到这一点。
你还需要弄清楚如何辨别什么是你想要的图像,而不是广告或导航元素、定位等。
您还需要了解如何成为一个好的网络公民,如果目标站点有robots.txt文件,则遵守该文件,如果您正在进行映像打包和掠夺,则限制您的请求,使其网络不会饱和并使其服务器陷入停顿,否则将被禁止。
发布于 2016-01-13 14:34:15
我是通过以下方式做到这一点的。我在这里张贴它可能对任何人都有用。
url="http://www.veromoda.in/elastic/search/fetch/?q=#{id}"
agent.get("#{url}")
links=agent.page.css('a').map { |k| k.attributes['href'].value }
if links.empty?
puts "Images for #{id} not available"
else
links.each do |l|
agent.get("#{l}")
agent.page.image_urls
image_links=agent.page.search('.lSPager').css('img')
image_links.each do |link|
image_name=File.basename(link.attributes["src"])
agent.get(link.attributes["src"]).save "vero_moda_images/#{id}/#{image_name}"
end
end
puts "Your #{id} Images Downloaded"
end
发布于 2016-01-07 00:38:57
您可以使用HTML来解析Nokogiri。使用selectorgadget来理解HTML中的标签、结构和模式,以解析和存储所需的数据。
https://stackoverflow.com/questions/34637767
复制相似问题