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

I am trying something new as I have a proxy to the internet for the first time. I need to download some JPG files and have written the following to do the work and it is downloading something but they are not valid JPG files. I am doing this on Win32.

Anyone have thoughts on what I am doing wrong?

#!perl.exe use strict; use WWW::Mechanize; use HTTP::Cookies; use LWP; use LWP::DebugFile; require HTTP::Request; my $cookie_jar = HTTP::Cookies->new( file => 'cookies.dat', autosave => 1, hide_cookie2 => 1 ); my $bot = WWW::Mechanize->new; $bot->max_redirect(100); $bot->cookie_jar($cookie_jar); $bot->proxy('http', 'http://proxy.company.com:80/'); $bot->proxy('https', 'http://proxy.company.com:443/'); $bot->no_proxy('localhost', 'company.com'); $bot->credentials('DOMAIN\user','pwd'); $bot->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0. +3) Gecko/20060426 Firefox/1.5.0.3'); my $x = 0; foreach $x (1..15) { my $url = "http://www.website.com/image$x.jpg"; print "Processing: $url\n"; my $response = $bot->get($url); my $content = $bot->content; open F, ">image$x.jpg"; print F $content; close F; }

Replies are listed 'Best First'.
Re: retrieving JPG file with LWP and WWW:Mechanize
by Corion (Patriarch) on Dec 26, 2006 at 17:39 UTC

    You want to use binmode before writing your file. But maybe you want to make LWP::UserAgent do all the work by doing the following instead:

    $bot->get($url, ':content_file' => "image$x.jpg");

    This will directly save the image to disk without further action on your part needed.

      I guess it is back to the manual to see how I missed this.

      I will probably do the binmode option since the code is already written but will definitely remember this little trick in the future.

Re: retrieving JPG file with LWP and WWW:Mechanize
by ferreira (Chaplain) on Dec 26, 2006 at 17:39 UTC

    If you are using Win32, to save a binary file, you need

    binmode F;
    right after open. See binmode.
      Yes, of course. I will read more on binmode again to keep it fresh in my mind.

      Thanks.