Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Overcoming image lock WWW::Mechanize

by jonnyfolk (Vicar)
on Feb 13, 2009 at 11:40 UTC ( #743586=perlquestion: print w/replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get property details at the following url, and getting into a bit of a pickle!

I am trying to access a site where the image is clearly only allowed with the session cookie. I'm using Mechanize to grab the cookies and the image url:

my $mech = WWW::Mechanize->new( cookie_jar => { file => 'cookies.txt', ignore_discard => 1, autosave => 1 } ); $mech->get($add); my $r = $mech->content; my @names; my @links = $mech->find_all_images(url_regex => qr/jpg/); for my $img (@links) { my $a = $img -> url_abs; push @names, $a; }

I'm currently using getstore( ... ) from LWP::Simple to write the image to file

I have tried using LWP::Useragent, $ua->mirror( $url, $filename ) after loading the cookie file: $ua -> cookie_jar('cookies.txt');, however this has not worked and the photo files are not recreated. If anyone can give me some pointers on how to do this I'd be very grateful.

Replies are listed 'Best First'.
Re: Overcoming image lock WWW::Mechanize
by Corion (Pope) on Feb 13, 2009 at 11:45 UTC

    Why don't you download the images using the WWW::Mechanize object you already have? Each WWW::Mechanize object already is-a LWP::UserAgent object, so it doesn't make sense to me to use a different LWP::UserAgent object if you want to save data. Maybe it would also help if you showed the code that doesn't work instead of the code that works.

      Oh, that's what I didn't understand. I changed my $ua to $mech and the images came booming through bright and clear. Thank you very much!!

      Update: Sorry, to be clear, for the record:

      my $mech = WWW::Mechanize->new( cookie_jar => { file => 'cookies.txt', ignore_discard => 1, autosave => 1 } ); $mech->get($add); my @links = $mech->find_all_images(url_regex => qr/jpg/); my $countphotos; for my $img (@links) { $countphotos++; my $photofile ="photo" . $countphotos . '.jpg'; my $a = $img -> url_abs; $mech->mirror( $a, $photofile ); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://743586]
Approved by marto
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-11-18 09:24 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.