Hello Alan,
Thanks a lot :)
I just copied your script, and added a get on the image link. It gives me
success and I can save the content. ( Pls see attached script ), but what I
receive in the file is not a picture but the complete page :(

I surely missing something very simple! :((

Dhanashri
 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, December 20, 2005 11:33 PM
To: beginners@perl.org
Subject: re: WWW::Mechanize, save images from a page

(I had tried to reply via nntp news which didn't work.  I just now
subscribed to
the emailing list. this is emailed to the list).

[EMAIL PROTECTED] (Dhanashri Bhate) writes:
> I tried using the find_image function to get the reference of the image (
as
> shown in the script below ).
> 
> But do not understand how I can save that image in a file.

I didn't know how to do it.  But in looking at the doc:

<quote>
$mech->find_image()

Finds an image in the current page. It returns a WWW::Mechanize::Image
object which describes the image. If it fails to find an image it
returns undef.
</quote>

That quote from doc does not call/describe what's returned as an image.

Also, in further looking at doc about what, in that mentioned *object*
is, "describes/description of the image" I find some various looks to
me like html markup items can be returned but includes a url function
or method or whatever the appropriate term (I know enough to be
dangerous).

Sure enuff, run the ref function shows type of reference.

Run the url method call (if that's the right name for it) brings url.

Piece together the url to where the image is at.  (the 10 year birthday
image at
the top left of the cpan web page).

Then download the image (Perl's LWP or wget if have it installed).

OP's code (modified in between the two ################### lines):

#!/usr/bin/perl -w
use strict;
use diagnostics;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->quiet(0);                #turn on warnings

my $host = "search.cpan.org";
my $url = "http://$host/~petdance/WWW-Mechanize-1.16/";;
$mech->get($url);
my $imageref = $mech->find_image( url_regex => qr/cpan-10/ );

#################################################
my $ref_type;
my $img_link;
if ( $mech->success ) {
$img_link=$imageref->url();
$ref_type=ref $imageref;
    print "Found the image the type of reference is : $ref_type\n";
print "the image link is: $img_link\n";
print "url to image is: http://$host$img_link\n";;
# view that url in a web browser and/or
# download here will get/put to hard drive
# `wget http://$host$img_link`; # forbidden, dunno why
# system ("wget http://$host$img_link";); # forbidden
##################################################

}
else
{
    print "Did not find the image\n";

} # end

-- 
Alan


---- Msg sent via CWNet - http://www.cwnet.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Attachment: get_image.pl
Description: Binary data

<<attachment: pic.jpg>>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to