1: # A program to download a lot of images from a site
   2: # quickly using LWP::Simple.  Assuming you have appropriate
   3: # permission, etc to download the images, and that the
   4: # images have sequential names (like img_001.jpg, 
   5: # img_002.jpg...), this program will load those images 
   6: # into the current folder.  You have to have the 
   7: # LWP::Simple module for this to work. -tim allen
   8: 
   9: use LWP::Simple;
  10: use strict;
  11:  
  12: # first part of the URL of the image
  13: # replace the URL and path info here appropriately
  14: my($pix)='http://tim/images/img_';  
  15:  
  16: for (my $i=1;$i<100;$i++) {
  17:   # this bit assumes the numbers of the files are zero
  18:   # padded to three zeroes (thanks btrott for sprintf
  19:   # pointer)
  20:   my $f = sprintf '%03d',$i;
  21:   # create the full URL of the image
  22:   # (like http://www.somesite.com/images/img_001.jpg)
  23:   my $url = $pix . $f . ".jpg";
  24:   # If we get the header, the image file exists
  25:   if (!head($url)) {
  26:     warn "sorry, $url doesn't exist\n";
  27:   } else {
  28:     # it exists, so get it and store it
  29:     getstore($url,"img_$f.jpg") 
  30:       or warn "can't get image: $!";
  31:     # Print a progress message
  32:     print "$url successfully stored\n";
  33:   }
  34: }
  35: print "Done!\n";

Replies are listed 'Best First'.
Re: Use LWP::Simple to download images from a website
by btrott (Parson) on Jan 16, 2001 at 22:14 UTC
    You wrote:
    > # this bit assumes the numbers of the files are zero > # padded to three zeroes... there is surely a better > # way to do this > my $f = substr("000".$i,-3,3);
    You were right, there is. :) Use sprintf:
    my $f = sprintf "%03d", $i;
    By the way, you might want to take a look at Image::Grab. Give it a web page URL, it'll grab it, parse it and look for images, and grab the images.

      Thanks, btrott! I've changed the number padding bit to use sprintf (doh!).

      This script is a little different from what I understand Image::grab to do. My script isn't set up to get images based on them being part of a web page. It's really so that someone with permission can download a sequence of images, like if a business partner shares all his or her product images with you, and tells you just to get them from the image directory on his or her web page.

      Thanks again! -tim