Created
August 14, 2013 17:16
-
-
Save kfox/6233205 to your computer and use it in GitHub Desktop.
A quick Ruby script to download PNG, GIF, or JPEG images from a given URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# usage: saveimages.rb <url> | |
# locally save all images from a web site | |
require 'nokogiri' | |
require 'open-uri' | |
exit if ARGV[0].nil? | |
doc = Nokogiri::HTML(open ARGV[0]) | |
images = doc.xpath('//img').to_a.reject! do |img| | |
url = img.attr('src') | |
# ignore images unless they're png, gif, or jpeg | |
# and ignore any images from quantserve | |
not url.match(/(png|gif|jpe?g)$/i) or url.match(/quantserve/) | |
end | |
images.each do |img| | |
url = img.attr('src') | |
filename = File.basename(url) | |
open(filename, 'wb') do |file| | |
puts "writing #{url} to #{filename}" | |
file << open(url).read | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment