Have a capybara script that among other things downloads absolute image links.

When trying to write those images to disk I receive an error:

File name too long

The output also includes a long list of all the image URLs in the array. I think a gsub would solve this but I'm not sure which one or exactly how to implement it.

Here are a few sample image URLs that are part of the link array. A suitable substitute name would be g0377p-xl-3-24c1.jpg or g0371b-m-4-6896.jpg in these examples:

http://www.example.com/media/catalog/product/cache/1/image/560x560/ced77cb19565515451b3578a3bc0ea5e/g/0/g0377p-xl-3-24c1.jpg
http://www.example.com/media/catalog/product/cache/1/image/560x560/ced77cb19565515451b3578a3bc0ea5e/g/0/g0371b-m-4-6896.jpg

This is the code:

require "capybara/dsl"
require "spreadsheet"
require 'fileutils'
require 'open-uri'

   def initialize
     @excel = Spreadsheet::Workbook.new
     @work_list = @excel.create_worksheet
     @row = 0
   end

       imagelink = info.all("//*[@rel='lightbox[rotation]']")
       @work_list[@row, 6] = imagelink.map { |link| link['href'] }.join(', ')
       image = imagelink.map { |link| link['href'] }
       File.basename("#{image}", "w") do |f|
         f.write(open(image).read)
       end
有帮助吗?

解决方案

You can use File.basename to get just the filename:

uri = 'http://www.example.com/media/catalog/product/cache/1/image/560x560/ced77cb19565515451b3578a3bc0ea5e/g/0/g0377p-xl-3-24c1.jpg'
File.basename uri  #=> "g0377p-xl-3-24c1.jpg"

其他提示

There is a real problem with the creation of filename.

imagelink = info.all("//*[@rel='lightbox[rotation]']")

Will return an array of nodes.

From that you get the href value using map and save the resulting array in image.

Then you try to use that array as the name of the file.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top