【问题标题】:Perl WWW::Mechanize CredentialsPerl WWW::Mechanize Credentials
【发布时间】:2011-07-15 01:55:56
【问题描述】:

我想使用 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');

【问题讨论】:

  • 顺便问一下你为什么使用$cookie_jar? Cookie 的东西在 Mechanize 中默认启用,所以我确定你不需要在这里使用它。

标签: perl


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 你的意思是像我发布的更新,因为我仍然无法登录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多