Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Website log-in with Perl and Mechanize

So this is driving me crazy. Basically, when I hard-code my user name and password, I can log-in no problem. But I want to prompt the user to enter the username and password, as I would like to share this program with others. (the program is supposed to log into our courses site and download all of our course work info - lectures, hw, etc)

This code works:

use WWW::Mechanize;
use LWP;

my $username = 'user'; 
my $password = 'pass';

my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());
$mech -> get('log-in url');
$mech -> form_name('theform');
$mech -> field ('username' => $username);
$mech -> field ('password' => $password);
$mech -> click ('log in');
print $mech-> content();

however, when I try and prompt the user to enter log-in info, it does now work. printing content returns the html of the log-in page, not the following page (courses page for said user)

use LWP;
use WWW::Mechanize;

my $login_url = 'log-in url';
print "\nUser name: ";
my $username = <>;
print "Password: ";
my $password = <>;

my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());
$mech -> get($login_url);
$mech -> form_name('theform');
$mech -> field ('username' => $username);
$mech -> field ('password' => $password);
$mech -> click ('log in');
print $mech-> content();

this really makes no sense since they are essentially the same thing. I even typed in the username/password in quotes in the prompt and still no avail..... (i realize also that it wont be very easy to check without a website and log-in info, sorry about that)

like image 808
msikd65 Avatar asked May 20 '11 18:05

msikd65


People also ask

How to login to a website using mechanize module in Python?

Your login credentials for that particular website. Thats all what it takes for us to login to a website using mechanize module in python. Once logged in you can have access to any authorized url (s) under that domain.

How to perform page fetches in WWW?

WWW::Mechanize supports performing a sequence of page fetches including following links and submitting forms. Each fetched page is parsed and its links and forms are extracted. A link or a form can be selected, form fields can be filled and the next page can be fetched.

Does WWW::mechanize support LWP::userAgent?

WWW::Mechanize is a proper subclass of LWP::UserAgent and you can also use any of LWP::UserAgent 's methods. Please note that Mech does NOT support JavaScript, you need additional software for that. Please check "JavaScript" in WWW::Mechanize::FAQ for more. The queue for bugs & enhancements in WWW::Mechanize.

What are some good examples of Perl modules for banking?

Here’s an example: I do my banking online, but get quickly bored with having to go to my bank’s site, log in, navigate around to my accounts and check the balance on each of them. One quick Perl module ( Finance::Bank::HSBC) later, and now I can loop through each of my accounts and print their balances, all from a shell prompt.


2 Answers

You need to run chomp() on the input from the user:

my $username = <>;
chomp($username);

The text supplied by the user has a carriage return at the end, which is screwing up your login.

like image 116
CanSpice Avatar answered Oct 05 '22 23:10

CanSpice


Although CanSpice is correct, you may also want to look at Term::ReadPassword, it provides the prompt, it hides the input AND it takes care of the chomp for you!

like image 40
Joel Berger Avatar answered Oct 05 '22 22:10

Joel Berger