首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用www:Mechanize登录站点

无法使用www:Mechanize登录站点
EN

Stack Overflow用户
提问于 2012-05-17 13:39:50
回答 1查看 439关注 0票数 2

我正在使用WWW:Mechanize来尝试登录一个站点。

代码

代码语言:javascript
复制
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();

但是,它并未登录到该站点。我做错了什么。该网站将重定向,并表示请启用cookies以继续。我该怎么做呢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-17 13:50:08

试着把这个块放在get之前。

代码语言:javascript
复制
$mech->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
    )
);

SuperEdit2:我自己也试过了,看起来很管用。试一试。(将表单编号改为3,并添加了代理别名)

代码语言:javascript
复制
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;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10630396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档