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 = "http://www.facebook.com";
# credentials
my $username = 'Enter User Name';
my $password = 'Enter Password';

my $mech_request;

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

$mech_request->form_name('menubar_login');
$mech_request->field(email => $username);
$mech_request->field(pass => $password);
$mech_request->click();

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);
$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 = "http://www.facebook.com/games?ap=1";
# fb_pages($page_url);

-----------------------------------------

 

pixel Facebook login using Perl and Mechanize

Leave a Comment

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

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge