Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How do i save an image with www::mechanize

by jonnyfolk (Vicar)
on Jan 20, 2009 at 04:34 UTC ( [id://737473]=note: print w/replies, xml ) Need Help??


in reply to How do i save an image with www::mechanize

Grab the html:
my $mech = WWW::Mechanize->new(); $mech->get($add); my $r = $mech->content;
perform a regex on $r to extract the url of the image, and then use either use Image::Magick or GD to save the image on your server.
$r =~ m#regex(.*)# my $imgurl = $1; my ($image, $x); my $photofile ="photo.jpg; $image = Image::Magick->new; $x = $image->Read($imgurl); $x = $image->Write("$photofile");

Replies are listed 'Best First'.
Re^2: How do i save an image with www::mechanize
by Anonymous Monk on Jan 20, 2009 at 08:56 UTC
    Or
    my $img = $mech->find_image( alt_regex => qr/captcha/i, url_regex => qr/Captcha/i, ); if( $img ){ $mech->get( $img->url, ':content_file' => 'captchar.jpg'); }
Re^2: How do i save an image with www::mechanize
by wilsond (Scribe) on Jan 20, 2009 at 08:20 UTC
    # after you get the URL (like [jonnyfolk] shows above) use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $res = $ua->mirror($imageurl, $localfilename);

    If you want to do evil, science provides the most powerful weapons to do evil; but equally, if you want to do good, science puts into your hands the most powerful tools to do so.
    - Richard Dawkins
      Why wouldn't you just use the mechanize object?

        I don't use WWW::Mechanize and don't really know it well. My way is just one more way to do it, though I admit probably not the best way.


        While I ask a lot of Win32 questions, I hate Windows with a passion. That's the problem with writing a cross-platform program. I'm a Linux user myself. I wish more people were.
        If you want to do evil, science provides the most powerful weapons to do evil; but equally, if you want to do good, science puts into your hands the most powerful tools to do so.
        - Richard Dawkins
Re^2: How do i save an image with www::mechanize
by missingthepoint (Friar) on Jan 21, 2009 at 01:50 UTC
    perform a regex on $r to extract the url of the image

    That's fragile and error-prone. Much better to use WWW::Mechanize's built-in methods, since they actually parse the returned HTML:

    $mech->get($add); my $img_obj = $mech->find_image( url_regex => qr{captcha\.php} ); $mech->get( $img_obj->url, ':content_file' => 'captchar.jpg' );

    ... or somesuch

    Update: sorry, somehow (!) didn't see Anonymonk's post


    Life is denied by lack of attention,
    whether it be to cleaning windows
    or trying to write a masterpiece...
    -- Nadia Boulanger

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-29 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found