dime has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get picture results from yahoo through our firewall. For the yahoo image search results page, this works:

my $ua = LWP::UserAgent->new; $ua->proxy('http', $proxyname); my $req = HTTP::Request->new('GET', $url); my $page = $ua->request($req)->content;

Now I would like to store the actual pictures that come with the search results in a file, not just the urls. I've never stored pictures before and am not sure how to combine picture retrieval with getting through the firewall. I've tried basically the same as above, but with the url for the picture, and handed the results over to getstore. That does not work; the return format of Request obviously is not a url. So how to do this? Any help would be appreciated. - Dime

Replies are listed 'Best First'.
Re: Get picture through firewall
by Tanktalus (Canon) on Aug 26, 2009 at 03:26 UTC

    It'd help if you posted what you tried, even if it doesn't work, because it'd give us a better idea of what you're thinking than simple English prose.

    Have you tried writing $page to your file?

    my $ua = LWP::UserAgent->new; $ua->proxy('http', $proxyname); my $req = HTTP::Request->new('GET', $url); my $page = $ua->request($req)->content; open my $fh, '>', $filename or die "Can't write to $filename: $!"; binmode $fh; # required on Windows, OS/2, DOS; doesn't hurt anywhere e +lse print $fh $page; close $fh;
    (Calling binmode is only one way to do that piece of work.) If this isn't what you mean, I'd need more insight into what you're trying to do.

      I had not. Thanks for this answer, it works well. Didn't think it could be that easy. ;D

Re: Get picture through firewall
by Gangabass (Vicar) on Aug 26, 2009 at 03:24 UTC

    Try this:

    my $ua = LWP::UserAgent->new; $ua->proxy('http', $proxyname); my $req = HTTP::Request->new('GET', $url); my $image = $ua->request($req)->content; open my $fh, ">", "picture.jpg" or die $!; binmode $fh; print $fh $image; close $fh;

    Of course picture extension may be not JPG...

    Also binmode is useful only on Windows.

Re: Get picture through firewall
by alexm (Chaplain) on Aug 26, 2009 at 12:28 UTC

    Be careful also with $ua->request($req)->content since you may want decoded_content instead. See HTTP::Message for the details.

Re: Get picture through firewall
by ig (Vicar) on Aug 27, 2009 at 08:47 UTC

    It won't help you write Perl, but if all you want to do is retrieve the pictures you may find wget useful. It is available on Windows, Linux and other platforms.