http://www.perlmonks.org?node_id=743586

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 (Patriarch) 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 ); }