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

Downloading and saving images.

by Anonymous Monk
on Sep 07, 2006 at 04:48 UTC ( [id://571599]=perlquestion: print w/replies, xml ) Need Help??

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

I am tring to download images from a website and save them on my hard drive. The method I am using only results in corrupted images that will not open. Im new to Perl and I cant seem to find any online resources to help me with my problem. This is what I have so far:

require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get('url_to_some_picture.jpg'); open(outfile, '>file_path_name'); if ($response->is_success) { print outfile $response->content; } else { die $response->status_line; }

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: Downloading and saving images.
by BrowserUk (Patriarch) on Sep 07, 2006 at 04:58 UTC

    Try using binmode on the output file:

    require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get('url_to_some_picture.jpg'); ## Either use :raw on the open open(outfile, '>:raw', 'file_path_name'); ### You should check for suc +cess! ## Or use bimode binmode outfile; if ($response->is_success) { print outfile $response->content; } else { die $response->status_line; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you both very much, works beautifully now. I'll take some time and research what binmode does.
        I'll take some time and research what binmode does

        On operating systems with Unix-like filesystem semantic, it does nothing.

        On some operating systems, however, you can't (successfully) write bytes with the eighth bit set certain byte sequences unless you use binmode. The most common operating system that exhibits this behavior is Microsoft Windows.

        update: Oops, got confused with a different issue.


        Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.
Re: Downloading and saving images.
by jZed (Prior) on Sep 07, 2006 at 04:56 UTC
    Try this after the call to open() and before the print:
    binmode outfile;
Re: Downloading and saving images.
by b10m (Vicar) on Sep 07, 2006 at 07:19 UTC

    While "the right answer" has already been given, you also might consider switching to LWP::Simple, for it makes your code a whole lot shorter ;)

    use strict; use LWP::Simple qw/getstore/; getstore("url_to_some_picture.jpg", "file_path_name");
    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Downloading and saving images.
by zentara (Archbishop) on Sep 07, 2006 at 13:30 UTC
    Here is a nice use of the "mirror" method, from a post by John Krahn. It handles error messages nicely
    #!/usr/bin/perl use warnings; &get_web_file; sub get_web_file { use Fatal qw(open close); use LWP::Simple; use HTTP::Status; my $URL = 'http://zentara.net/2uni2.jpg'; my $localfile = '2uni2.jpg'; my $rc = mirror( $URL, $localfile ); warn status_message( $rc ) if is_error( $rc ); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found