1
#!/usr/bin/perl

#USE DECLARATIONS
use strict;
use warnings;
use WWW::Mechanize;
use Term::ANSIColor;

#VARIABLE DECLARATIONS
my $mech = WWW::Mechanize->new();
my $img;
my $title;
my $pic_page;
my $url;
my $count;
my @links;

#CONNECT TO FACEBOOK
$url = 'https://www.facebook.com/';
$mech = WWW::Mechanize->new();
$mech->agent_alias( 'Linux Mozilla' );
$mech->get( $url );
$title = $mech->title();

#LOGIN FORM
print "Connected to Facebook.\n";
print "Logging in...";
$mech->form_id("login_form");
$mech->field("email",'my@email.com');
$mech->field("pass",'mypass');
$mech->click();
print "done!\n";

#NAVIGATE TO USER PAGE
$mech->get("https://www.facebook.com/some.profile1234");
$title = $mech->title();
print "Finding $title 's profile pictue...\n";

#FIND PROFILE PICTURE
$img = $mech->find_image(url_regex => qr/s160x160/, );
print $img->url();
downloadImage($img->url(),$mech->title().".jpg");

sub downloadImage
{
    my $local_file_name = $_[1];
    my $b = WWW::Mechanize->new;
    print "Downloading: $_[1]...";
    $b->get( $_[0], ":content_file" => $local_file_name );
    print "done!\n";
}

使用此代码,我只想下载给定人的个人资料图片(#NAVIGATE TO USER PAGE)并保存。但是,我收到一个错误,说基本上找不到图像!为什么?(我正在使用 $mech->find_image(url_regex => qr/s160x160/,) 在个人资料页面上查找图像。)

4

1 回答 1

0

您正在 downloadImage 子中使用新的 Mechanize 实例。而且此实例未经 Facebook 授权

尝试这个:

downloadImage($img->url(),$mech->clone() );

sub downloadImage
{
    my $mech = $_[1];
    print "Downloading: $_[1]...";
    $mech->get( $_[0], ":content_file" => $mech->title() . ".jpg" );
    print "done!\n";
}
于 2013-03-17T00:42:40.683 回答