Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: save image on web

by silent11 (Vicar)
on Dec 21, 2004 at 04:18 UTC ( #416380=note: print w/ replies, xml ) Need Help??


in reply to save image on web

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


Comment on Re: save image on web
Download Code
Re^2: save image on web
by Anonymous Monk on Dec 21, 2004 at 04:45 UTC
    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();
        Got the following message when trying to run saskaqueer's code:

        LWP will support https URLs if the Crypt::SSLeay module is installed. More information at <http://www.linpro.no/lwp/libwww-perl/README.SSL>.

        I tried installing Crypt::SSLeay, but ppm3 does not have a win32 version.

        As for NetWallah's suggestions, I have a direct URL to the image, so it is not in a frame. It is also under 4 MB, not generated by javascript and not beign re-directed. I can download it manually just fine. I would post the URL, but it would not help since it is part of a password protected site and it would just redirect you to a login.
      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
        I tried WWW::Mechanize and it gave me a blank image. There is something in the web server daemon that is preventing me from downloading through a request/response. I can only save off the image if I do it manually. I was able to save the image by using a hack solution with Win32::GuiTest. The module allows me to automate key presses as if I were typing them.
        use Win32::GuiTest qw(SendKeys); $IE->Navigate( $url ); <wait> SendKeys("%f$path~");
        I wish to revisit the OLE automation and ExecWB command. I don't understand why this does not work. The following snippet should work, but it never saves the image unless I show the dialog and select the path manually.
        $IE->Navigate( $url ); while( $IE->{Busy} ) { while($IE->SpinMessageLoop()) { select undef, undef, undef, 0.25; } } $var = Variant( VT_BSTR, "./image.jpg" ); # 4 = SAVE_AS; 3 = HIDE_DIALOG $IE->ExecWB( 4,3,$var,undef );
        PS: For some reason I can't see my last post, so here it is again:

        Got the following message when trying to run saskaqueer's code:

        LWP will support https URLs if the Crypt::SSLeay module is installed. More information at <http://www.linpro.no/lwp/libwww-perl/README.SSL>.

        I tried installing Crypt::SSLeay, but ppm3 does not have a win32 version. As for NetWallah's suggestions, I have a direct URL to the image, so it is not in a frame. It is also under 4 MB, not generated by javascript and not beign re-directed. I can download it manually just fine. I would post the URL, but it would not help since it is part of a password protected site and it would just redirect you to a login.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-09-23 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (241 votes), past polls