Perl WWW :: Mechanize Credentials

时间:2011-07-15 01:55:57

标签: perl

我想使用WWW :: Mechanize从网站搜索内容但是为了能够首先执行此操作我必须使用我的注册用户名和密码登录,我无法使用此代码。必须更改的内容才能成功提交表单。感谢。

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";

$mech->submit_form(
   form_number => 4,
);
die "Submit failed" unless $mech->success;

$mech->save_content('log.txt');

更新:

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');

2 个答案:

答案 0 :(得分:0)

该页面不使用HTTP身份验证,这是credentials的用途。只需使用Mech填写usernamepassword表单字段,然后提交登录表单。

答案 1 :(得分:0)

你在这里不需要credentials。只需使用:

$mech->submit_form(
   form_number => 4,
   fields => {
        username => $user,
        password => $pass,
   },
);

当然,不要忘记将usernamepassword更改为目标网页的实际字段名称。