2

我正在使用 WWW:Mechanize 尝试登录网站。

代码

use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

$mech->get("https://www.amazon.com/gp/css/homepage.html/");
$mech->submit_form(
form_name => 'yaSignIn',
fields => {
email => 'email',
qpassword=> 'pass'
}
);


print $mech->content();

但是,它没有被登录到该站点。我究竟做错了什么。网站重定向并说请启用 cookie 以继续。我怎么做 。

在此处输入图像描述

4

1 回答 1

5

试着把这个块放在你得到之前。

$mech->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
    )
);

SuperEdit2:我自己尝试过,它似乎有效。试试看。(将表格编号更改为3并添加了代理别名)

use strict;
use warnings;
use WWW::Mechanize;

# Create a new instance of Mechanize
my $bot = WWW::Mechanize->new();
$bot->agent_alias( 'Linux Mozilla' );
# Create a cookie jar for the login credentials
$bot->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
    )
);
# Connect to the login page
my $response = $bot->get( 'https://www.amazon.com/gp/css/homepage.html/' );
# Get the login form. You might need to change the number.
$bot->form_number(3);
# Enter the login credentials.
$bot->field( email => 'email' );
$bot->field( password => 'pass' );
$response = $bot->click();

print $response->decoded_content;
于 2012-05-17T05:50:08.227 回答