Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GET images 2

Status
Not open for further replies.

jvchamary

Programmer
Feb 4, 2004
9
0
0
CH
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.

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);
}
 
Thank you for your comments (& the star!)

It's not easy but O'Reilly - Perl & LWP / Sean M. Burke is what I would advise


Kind Regards
Duncan
 
I'd agree with Duncan, Sean's Perl & LWP is a great book, with some very good examples on how to do all this sort of thing. To help simplify some of the guts, you might be also interested in WWW::Mechanize, which comes with some cookbook examples. I've started using this more and more and has reduced alot of the boring code.

Barbie
Leader of Birmingham Perl Mongers
 
i must say (probably 'cos i'm stoned!) that it is so refreshing to see someone (i.e. jvchamary) give an actual URL rather than some imaginary !@£$%! ... makes answering considerably easier!!!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top