Technology Feed Lab

technology site

Facebook login using Perl and Mechanize

Posted by TechGeek on June 28, 2011

This article will explain how to login into Facebook using Perl language? We will be using WWW::Mechanize again for this. You can use mechanize library for automating interaction with websites. The mechanize supports lot of different methods to handle text, HTML or images. It also automatically stores cookies, follow redirections etc. You can even parse links, follow links or submit web based forms easily.

Accessing the Facebook homepage requires you to be a valid user and must be logged in to do further activity. Simply fetching a webpage will not work as it will redirect you to a login page since you are not authenticated to Facebook. So you need to store credential cookies and will have to send that cookies on each requests. For this, we will use another Perl module called HTTP::Cookies. The method will be,

$cookie_jar = HTTP::Cookies->new

The above method will save our cookies. Here is the complete code for Facebook login,
#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;
use HTTP::Cookies;

my $url = "";
# credentials
my $username = 'Enter User Name';
my $password = 'Enter Password';

my $mech_request;

sub login
$mech_request = WWW::Mechanize->new(autocheck => 0);
my $res = $mech_request->get($url);

$mech_request->field(email => $username);
$mech_request->field(pass => $password);

my $contents = $mech_request->content();
return $contents;

#access Facebook URL's after Login
sub fb_pages()
my $url = shift;
my $res = $mech_request->get($url);
my $data = $mech_request->content();
print $data;

# Start - Login to Facebook
my $data = login();
print $data;

# Now you have logged in and saved a cookie
# access anything

#my $page_url = "";
# fb_pages($page_url);



Leave a Comment

Your email address will not be published. Required fields are marked *