Raw materials needed for this script are:
1. The URL of the sign-in page. Shoot up your browser, go to the website you want access, go to the login screen and copy the URL address.
2. The name/number of the html login form:
To obtain this, launch the sign in URL in a browser window, view source of the page.
Look for the block that starts with <form, the value of the "name" property is the form name ( if exists )
If there is no form name:
If only one form is found in the source code, the form number will be 1
If there are multiple forms and the login form is N-th one in the list, N is the form number.
3. The value of the "name" property corresponding to the input elements for username and password in the form.
With these details, the below mentioned code can log you into the page. Comments are added above the code lines, wherever necessary.
#!/usr/bin/perl -w use strict; ## WWW::Mechanize is a wrapper on LWP:UserAgent object, ## which extents the browser capabilities of LWP. use WWW::Mechanize; ## Needed for initializing and storing cookies for the website use HTTP::Cookies; ## Needed if your code has to support SSL use Crypt::SSLeay; ## Use only if you want to trouble shoot!! use LWP::Debug qw(+); ## I want to login to Ubuntu forum, so this is my URL, my $URL = "http://ubuntuforums.org"; ## My Ubuntu forum username and password my $username = "your_username"; my $password = "your_password"; ## Create a browser and a cookie object my $browzer = WWW::Mechanize->new(); $browzer->cookie_jar(HTTP::Cookies->new()); ## Open ( HTTP GET ) the login URL $browzer->get($URL); ## Ubuntu forum has no form name, so using the number "2" ## as it is the second form on the login page, ## first being a forum search box. $browzer->form_number(2); ## vb_login_username/vb_login_password are the input names ## used in this form $browzer->field(vb_login_username => $username); $browzer->field(vb_login_password => $password); ##Submit the form $browzer->submit(); ## If you are not automatically redirected to the ## forum home page after loging in, ## emulate clicking the link that says ## "Click here if your browser does not automatically ## redirect you." $browzer->follow_link( text => "Click here if your browser does not automatically redirect you." ); ## Now that you have logged in, you can use different methods of ## WWW::Mechanize to process the web pages. ## Eg: Save the content of the page you see after loging in ## my $output_page = $browzer->content(); ## my $output_file = "/tmp/mylogin.html" ## open(OUTFILE, ">$output_file"); ## print OUTFILE "$output_page"; ## close(OUTFILE);
No comments:
Post a Comment