Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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.

Comment on save image on web
Re: save image on web
by silent11 (Vicar) on Dec 21, 2004 at 04:18 UTC
    use LWP::Simple; use strict; my $URL = 'http://www.google.com/images/firefox/google.gif'; getstore($URL,'google.gif') || warn $!;

    Worked fine on WinXP.


    -silent11
    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 = 'http://www.server.com/path/to/image.gif'; 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 = 'http://www.google.com/images/firefox/google.gif'; my $mech = WWW::Mechanize->new(); $mech->get( $url ); open (OUT, ">$local_image"); binmode OUT; # Windows only print OUT $mech->content;


        -silent11
        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://416379]
Approved by Errto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-09-17 01:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls