How to release or kill Mechanize session

D

dysgraphia

I am using scripts like the following to retrieve web data.
After login I use another instance of $mech->get($url) to
get data from links on the retrieved page.
Sometimes I get a login failure which may be due to the page
timing out. Rather than have this happen I decided to deliberately
end each session after a few downloads then do the login again.
If I enclose my script with the statements perl.begin; and perl.end;
this seems to have worked.

My question: Is this the correct way to kill or release a Mechanize session?
Any comments or suggestions welcome...cheers, Peter

#!/user/bin/perl -w
use strict;
use warnings;

use WWW::Mechanize;
use DBI;
use HTTP::Cookies;
use Win32::ODBC;

# Do the login...
my ($url,$username,$password);
$url = 'http://www.xxxxxx.com/login.asp';
$username = "xxxx";
$password = "xxxx";
my $mech = WWW::Mechanize->new(
agent => 'Mozilla/4.0',
cookie_jar => {} );
$mech->get($url);
unless ($mech->success)
{
die "Can't get the login page $url: ",
$mech->response->status_line;
}
$mech->field(p_login => $username);
$mech->field(p_password => $password);
$mech->click();

my $url1 = 'http://www.xxxxxx.com/';

$mech->get($url1);
unless ($mech->success)
{
die "Can't get the login page url1 $url1: ",
$mech->response->status_line;
}

print "Now retrieving: "$url1,"\n";

# The retrieved page may have up to 10 links
# create an array of the links
my @links = $mech->links;
# loop through the links
foreach my $link (@links)
{
my $href = $link->url;
# Retrieve the link text
my $name = $link->text;
print "href: " , $href, "\n" , "name: " , $name , "\n" ;
sleep(3);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top