Downloading Images using Perl
In this section, we will see two approaches to download images using Perl scripts. In order to get the URL of these images, we first right-click on them. Next, we click on Copy Image Address from the drop-down and paste this as the URL for the image.
Downloading images using LWP::Simple
In this approach, we use LWP::Simple module and get the HTTP code using getstore function. In this function, we have to specify the URL of the image to be downloaded and the location to store the downloaded image. Next, we check if the code is successful or not and display the corresponding message to the user.
Perl
#!usr/bin/perl # using the strict pragma use strict; # using the warnings pragma # to generate warnings for # erroneous code use warnings; # specifying the Perl version use 5.010; # calling the module use LWP::Simple; # declaring the sub routine sub getImage { # displaying a user friendly message say "Downloading ... " ; # variable to store the status code # first parameter is the URL of the image # second parameter is the location # of the downloaded image my $statusCode = getstore "downloaded_image.png" ); # checking for successful # connection if ( $statusCode == 200) { say "Image successfully downloaded." ; } else { say "Image download failed." ; } } # calling the sub routine getImage(); |
Output:
Downloading... Image successfully downloaded. (the downloaded image will be saved at the specified location with the given name. If no location is specified then the image would be saved in the current working directory.
Downloading Images using Image::Grab Module
Image::Grab is a simple module meant for downloading the images specified by their URLs. It works with images that might be hidden by some method too. In this approach, we use the Image::Grab module and after instantiating it, we pass the URL. Next, we call the grab method and save the downloaded image to disk.
Perl
#!usr/bin/perl # using the strict pragma use strict; # using the warnings pragma to # generate warnings for erroneous # code use warnings; # specifying the Perl version use 5.010; # calling the Image::Grab module use Image::Grab; # instantiating the module # and storing it in a variable my $instantiatedImage = new Image::Grab; # declaring the sub routine sub getImage { # specifying the URL $instantiatedImage ->url # calling grab to grab the image $instantiatedImage ->grab; # creating a file to store # the downloaded image open (DOWNLOADEDIMAGE, '>downloaded_image1.png' ) || die 'downloaded_image1.png: $!' ; # for MSDOS only binmode DOWNLOADEDIMAGE; # saving the image in the created # file print DOWNLOADEDIMAGE $instantiatedImage ->image; # closing the file close instantiatedImage; } # calling the sub routine getImage(); |
Output:
The image is stored with the specified file name.
Downloaded Image:
Downloading Files from Web using Perl
Perl is a multi-purpose interpreted language that is often implemented using Perl scripts that can be saved using the .pl extension and run directly using the terminal or command prompt. It is a stable, cross-platform language that was developed primarily with strong capabilities in terms of text manipulation and modifying, and extracting information from web pages. It is under active development and open source. It finds major use in web development, system administration, and even GUI development due to its capability of working with HTML, XML, and other mark-up languages. It is prominently used along with the Web as it can handle encrypted web data in addition to E-Commerce transactions.
In this article, we will be seeing different approaches to download web pages as well as images using Perl scripts.