Perl WWW::Mechanize Credentials
I want to search content from a site using WWW::Mechanize but to be able to do that first I must be logged in with my registered username and password, which I can't do with this code. What must be changed so I can submit the form successfully. Thanks.
use strict;
use warnings;
use WWW::Mechanize;
my $username = "username";
my $password = "password";
my $cookie_jar;
my $url = "http://www.albumartexchange.com/forums/ucp.php?mode=login";
my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$mech->credentials($username, $password);
$mech->get($url);
$mech->success() or die "Failed";
开发者_如何学Go
$mech->submit_form(
form_number => 4,
);
die "Submit failed" unless $mech->success;
$mech->save_content('log.txt');
UPDATE:
use strict;
use warnings;
use WWW::Mechanize;
my $cookie_jar;
my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar );
$mech->get( 'http://www.albumartexchange.com/forums/ucp.php?mode=login' );
$mech->submit_form(
form_number => 4,
fields => {
'username'
=> 'some_username',
'password'
=> 'some_password',
}
);
$mech->save_content('log.txt');
You don't need credentials
here. Just use:
$mech->submit_form(
form_number => 4,
fields => {
username => $user,
password => $pass,
},
);
Of course don't forget to change username
and password
to the actual field names for your target page.
That page doesn't use HTTP authentication, which is what credentials
is for. Just use Mech to fill in the username
and password
form fields and submit the login form.
精彩评论