Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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.

Comment on Overcoming image lock WWW::Mechanize
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-09-20 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls