deckoff has asked for the wisdom of the Perl Monks concerning the following question:
Ok, guys, I am new to perl and the forum, i hope this is not a capital offence ;)
Here is the problem.
I want to login via perl script to http://www.tennisinsight.com/match_previews.htm and download the page. I am stuck at login into the site via the script. I want to save all cookies generated in the process, the site uses cookies.1 The site uses cookies to store login data.
2 The login form is triggered by javascript, but that is not important, because a simple web page on local server that containts only:
given the right username and pass will send the needed data, and the site will redirect to main website page, user will be logged, and cookies created. In short, a simple post with correct data is all needed from client side. 3.I have successfully tried and fetched the page I need with curl, once the correct cookies were provided. ( I have converted FF3 db file to FF2 txt file and provided ot to curl) I think that posting to myTI.php, storing the returned cookies and then opening the page I need while reading the cookies will allow me to Here is the script I use to get cookies, I now it prints to stdout atm, still unsuccessful.<form action="http://www.tennisinsight.com/myTI.php" method="POST"> <input name="username" type="text" size="25" /> <input name="password" type="password" size="25" /> <input name="mySubmit" type="submit" value="Submit!" /> </form>
Seems I am missing somethinguse warnings; use HTML::Tree; use LWP::Simple; use WWW::Mechanize; use HTTP::Request::Common; use Data::Dumper; my $username = "user"; my $password = "pass"; my $site_url = 'http://www.tennisinsight.com/myTI.php'; my $mech = WWW::Mechanize->new( autocheck => 1 ); # print $mech->content; my $response = $mech->post($site_url , [ 'username' => $username, 'password' => $password, ]) ; my $cookie_jar = HTTP::Cookies->new; $cookie_jar->extract_cookies( $response ); print $cookie_jar;
This works for me. After a bit of reading, and examining the the headers, I added keep alive to $mech and that solved it.#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; # use HTTP::Cookies; # use HTML::Form; my $urlLog = "http://tennisinsight.com/myTI.php"; my $url = "http://www.tennisinsight.com/match_previews.htm"; my $mech = WWW::Mechanize->new( autocheck => 1, keep_alive => 115, 'Ti +meout' => '30',); my $cookie_file = "cookies.txt"; my $username = "usr"; #username VALUE my $password = "pss"; #password VALUE my $inputUser = "username"; #username KEY NAME my $inputPassword = "password"; #password KEY NAME $mech->post($urlLog, [ $inputUser => $username, $inputPassword => $pas +sword ]); $mech->cookie_jar->save($cookie_file); if (! -s $cookie_file){ $mech->post($urlLog, [ $inputUser => $username, $inputPassword => +$password ]); # Save the cookie from this session $mech->cookie_jar->save($cookie_file); } else { $mech->cookie_jar->load($cookie_file); $mech->get("http://www.tennisinsight.com/match_previews.htm"); } # Print to file my $output = $mech->content(); open(my $fh, '>', 'report.html'); print $fh $output; close $fh; print "done\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Save cookies after post method, perl, mechanize, redirect
by zentara (Cardinal) on May 08, 2014 at 15:09 UTC | |
by deckoff (Novice) on May 08, 2014 at 18:34 UTC | |
|
Re: Save cookies after post method, perl, mechanize, redirect
by InfiniteSilence (Curate) on May 08, 2014 at 17:21 UTC | |
|
Re: Save cookies after post method, perl, mechanize, redirect
by Anonymous Monk on May 08, 2014 at 15:21 UTC |