Вопрос

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