Hello!
I primarily work with data in text files, but on occasion I download html files. Recently, I tried adapting one of my scripts so that I can download images from the web.
Unfortunately, while I can create files, they either can't be viewed or look really weird (sometimes one can just about make out what they're supposed to be).
Is there some sort of 'time-out' when downloading images or are they encoded in some way? If the latter is true, how could I go about making the image viewable? I know hardly anything about using Perl with the web, so please be gentle!
The script is below. Thanks in advance for your replies!
J.
I primarily work with data in text files, but on occasion I download html files. Recently, I tried adapting one of my scripts so that I can download images from the web.
Unfortunately, while I can create files, they either can't be viewed or look really weird (sometimes one can just about make out what they're supposed to be).
Is there some sort of 'time-out' when downloading images or are they encoded in some way? If the latter is true, how could I go about making the image viewable? I know hardly anything about using Perl with the web, so please be gentle!
The script is below. Thanks in advance for your replies!
J.
Code:
#!/usr/bin/perl -w
# 'download_images.pl'
@names = qw(Biologo.GIF montage2.JPG);
use HTML::Parse;
use LWP::UserAgent;
$useragent = new LWP::UserAgent;
$useragent -> proxy('http', '[URL unfurl="true"]http://wwwcache3.bath.ac.uk:3128/');[/URL]
foreach $name (@names) {
$url = "[URL unfurl="true"]http://www.bath.ac.uk/bio-sci/images/$name";[/URL]
$request = new HTTP::Request GET => $url;
print $request -> as_string();
$response = $useragent -> request($request);
if ($response -> is_success) {
$image = $response -> content;
output2file("$name", $image);
} else {
print $response -> error_as_HTML;
}
}
exit;
sub output2file {
my($OUT, $printout) = @_;
open (OUT, ">>$OUT");
print OUT "$printout";
close (OUT);
}