Home > Perl > Login To Google Using Perl

Login To Google Using Perl

In this case I was particularly interested in logging in, so that I could programmatically download my Financial Portfolios, to see information the way I’d like it. I though about building my own database, but why? Google does a fantastic job of doing this for me, and as long as I can grab it with Perl (The greatest language on Earth). Then what is the point of me managing my own MySQL tables, worring about its uptime etc.

So feel free to alter the code, but this will get you into Google. You will most likely just need to alter the url lines and put in your username and password info.

Here is the code:

You will need the following Modules

IO-Socket-SSL

WWW-MECHANZE

and HTTP::Cookies;

http://gregjessup.com/snippets/google_login.txt

#!c:\\perl\\bin
use strict;
use WWW::Mechanize;
use HTTP::Cookies;
###go to login page and login.
my $url = “https://www.google.com/accounts/ServiceLogin?hl=en&service=finance&nui=1&continue=http%3A%2F%2Ffinance.google.com%2Ffinance”;
my $username = “yourname@gmail.com”;
my $password = “yourpassword”;
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_number(1);
$mech->field(Email => $username);
$mech->field(Passwd => $password);
$mech->click();
#Go to the next link, now that we are logged in.
$url = ‘http://finance.google.com/finance/portfolio?action=view&pid=1&pview=pview&output=csv’;
$mech->get($url);
my $output_page = $mech->content();

#now you can do something with the content. Remember $output_page is a string :(

Greg Perl

  1. frito
    May 25th, 2009 at 23:39 | #1

    i get this error
    There is no form numbered 1 at googleLogin.pl line 12

  1. June 6th, 2008 at 14:49 | #1