简体   繁体   中英

filename too long when trying to read and write from an array of absolute images

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM