Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

save image on web

by kcella (Beadle)
on Dec 21, 2004 at 04:11 UTC ( #416379=perlquestion: print w/replies, xml ) Need Help??
kcella has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to find a module and/or OLE code to save an image on the web.

I have tried using the InternetExplorer.Application OLE and ExecWB method, but I can never get it to save. Any other modules I try (HTTP::GetImages, LWP::Simple) seem to fail.

I know the URL of the image I want to retrieve, but need a way to save it to disk. Keep in mind, I need to do this through Win32. Please help. thanks.

Replies are listed 'Best First'.
Re: save image on web
by silent11 (Vicar) on Dec 21, 2004 at 04:18 UTC
    use LWP::Simple; use strict; my $URL = ''; getstore($URL,'google.gif') || warn $!;

    Worked fine on WinXP.

    Spread Firefox
      May have found the reason. The code you gave me works, but when I substitute my URL it does not save. I can goto the page in the browser, but cannot access through code. I get the 501 response code. It seems as though the site I am trying to pull from disables such a request. Does anyone know of a hack to get around this? -kcella
        Just throwing out a few possibilities:

        • You may be loading a page that contains FRAMES, and the image may have a URL relative to a different frame than you expect.
        • The image may be Javascript-generated
        • The URL you have may result in a REDIRECT that displays the final image (Update)
        • The image may be over 4 Gb - IIS throws 501 errors in that case.
        You may need to capture packets and/or view the page source to get more information about this problem. It would also help if you posted the image URL.

            ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

        In addition to NetWallah's suggestions, the site may also be blocking according to the referrer URL, or by the user-agent of the "browser", or possibly even by a cookie (if the image URL is that of a CGI script). Try this code (which is completely untested):

        #!perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common; my $url = ''; my ($referer) = $url =~ m!^(https?://[^/]+/)!; my $ua = LWP::UserAgent->new( agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)', cookie_jar => {} ); # probably not needed, only needed if cookie is used # to test for "real browser". highly unlikely, but you # never know! $ua->get($referer); my $req = HTTP::Request->new( GET => $url ); $req->referer($referer); print $ua->request($req)->content();
        You never gave us the actual URL of the image you are trying to download so we are leading you blindly. This script works for me.
        use strict; use WWW::Mechanize; my $local_image = './google.gif'; my $url = ''; my $mech = WWW::Mechanize->new(); $mech->get( $url ); open (OUT, ">$local_image"); binmode OUT; # Windows only print OUT $mech->content;

        Spread Firefox
Re: save image on web
by kcella (Beadle) on Dec 22, 2004 at 03:49 UTC
    I am getting closer now...

    Searching on the web, I found and installed a ppd copy of Crypt::SSLeay and got just about all the different save methods to work. The problem is that the content I get back from a request is the member login page instead of the image data. My guess is that the corresponding "get" method is being seen as a new session to the server, so it is not using the current session I logged into through Win32::OLE. How can I use the OLE session and tie it to the "get" method of the save routine from the other module?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://416379]
Approved by Errto
[Corion]: Maybe the Perl you try stand-alone and the Perl you run are different?

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-23 12:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.