Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save all image files from a website

I'm creating a small app for myself where I run a Ruby script and save all of the images off of my blog.

I can't figure out how to save the image files after I've identified them. Any help would be much appreciated.

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = '[my blog url]'
doc = Nokogiri::HTML(open(url))

doc.css("img").each do |item|
  #something
end
like image 250
Zack Shapiro Avatar asked Oct 28 '11 08:10

Zack Shapiro


People also ask

How do you download multiple pictures at once?

You can hold down Shift and click further down the page to select multiple photos at once, or hover over a date and click the tick that appears to select all photos and videos from that day. Click on the three dots at the top right and click Download (or press Shift + D).


2 Answers

URL = '[my blog url]'

require 'nokogiri' # gem install nokogiri
require 'open-uri' # already part of your ruby install

Nokogiri::HTML(open(URL)).xpath("//img/@src").each do |src|
  uri = URI.join( URL, src ).to_s # make absolute uri
  File.open(File.basename(uri),'wb'){ |f| f.write(open(uri).read) }
end

Using the code to convert to absolute paths from here: How can I get the absolute URL when extracting links using Nokogiri?

like image 91
Phrogz Avatar answered Sep 28 '22 01:09

Phrogz


assuming the src attribute is an absolute url, maybe something like:

if item['src'] =~ /([^\/]+)$/
    File.open($1, 'wb') {|f| f.write(open(item['src']).read)}
end
like image 23
pguardiario Avatar answered Sep 28 '22 00:09

pguardiario