#!/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)并保存。但是,我得到一个错误,基本上没有找到图像! WHYYY? (我使用$ mech-> find_image(url_regex => qr / s160x160 /,)在个人资料页面上查找图片。)
答案 0 :(得分: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";
}