O'Reilly logo

Wicked Cool Ruby Scripts by Steve Pugh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Image Scrape

Image Scrape

imageScrape.rb

This script will scrape every image from the page at a user-supplied URL. The image files will include data residing on the host machine in addition to images linked from other web servers.

The Code

 require "open-uri"
 require "pathname"

 unless ARGV[0]
     puts "You must supply a URL to scrape images."
     puts "USAGE: ruby imageScrape.rb <url to scrape>"
     exit
 end

 url = ARGV[0].strip
 begin

     open(url, "User-Agent" => "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)")
 do |source|
         source.each_line do |x|
 if x =~ /<img src="(.+.[jpeg|gif])"\s+/ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required