Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How to get/save image with LWP

by cormanaz (Deacon)
on Jul 07, 2021 at 20:53 UTC ( #11134776=perlquestion: print w/replies, xml ) Need Help??

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

Good day fellow monks. I am having trouble with what I thought would be a simple task, getting a web image and saving it to a file using LWP. My first attempt:
use feature ':5.10'; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $response = $ua->get(' +ging_a_tube.jpg'); if ($response->is_success) { my $content = $response->decoded_content; open(OUT,">c:/temp/test.jpg") or die "Can't open output: $!"; binmode OUT; print OUT $content; close OUT; } else { say "Fail" }
resulted in a successful get and a file written with "wide character" warning, but the file was not renderable.

So then I tried another method that I thought was designed for this very task:

use LWP::Simple; my $success = getstore(' +ing_a_tube.jpg','c:\temp\test.jpg'); say $success;
It gave a completion code of 200, no wide char warning, but also an unrenderable file.

I would prefer to use LWP:UserAgent for this rather then LWP::Simple since I want to get the images in the context of some other web retrieval. What am I doing wrong?

Replies are listed 'Best First'.
Re: How to get/save image with LWP
by hippo (Bishop) on Jul 07, 2021 at 20:57 UTC
    my $response = $ua->get('');

    Have you looked at that URL? It isn't an image which is why it is "not renderable". Use the actual URL of the image:


      Good point. I saw it ended in .jpg so I assumed it was an image. Should have checked it manually. With the actual image link it works fine. Sorry for the cycles.
Re: How to get/save image with LWP
by marto (Cardinal) on Jul 07, 2021 at 21:46 UTC

    A mojo based proof of concept. Feel free to add error checking etc:

    #!/usr/bin/perl use strict; use warnings; use Mojo::UserAgent; my $url = ' +g'; my $ua = Mojo::UserAgent->new; my $dom = $ua->get( $url )->res->dom; my $target = $dom->at('#file > a:nth-child(1) > img:nth-child(1)')->at +tr('src'); $ua->get('https:' . $target)->res->save_to('target.jpg') ;
Re: How to get/save image with LWP
by bliako (Monsignor) on Jul 08, 2021 at 09:39 UTC

    In addiion to what others said, for raw content I would use $response->content;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2022-06-28 15:56 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (92 votes). Check out past polls.