我一直在使用 WWW::Mechanize perl 模块尝试登录需要 Steam 身份验证的网站。到目前为止,我已经能够成功地将它带到登录页面,但是一旦我提交了我的表单,它似乎根本不起作用。提交表单后,页面内容不会改变,就好像我登录失败一样,它要我再试一次。我已经搜索了几个小时,并且尝试了许多设置表单框和提交文本的不同组合,但没有任何效果。出于某种原因,我几乎没有发现有关检查 Mechanize 是否可以查看登录是否有效的信息。也许我的搜索条件不好。
这是我到目前为止得到的代码。这应该做的是转到 tf2wh 的登录表单,该表单重定向到 Steam 登录页面,填写用户/密码,单击按钮,然后返回 tf2wh,登录会话保存在 cookie 中。但是,唯一有效的部分是初始登录重定向。
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
my $user = "my_username";
my $pass = "my_password";
my $uri = 'http://www.tf2wh.com/?login';
my $cookies = 'cookies.txt';
my $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
my $mechanize = WWW::Mechanize->new(
agent => $agent,
cookie_jar => {},
autosave => 1,
ignore_discard => 1);
$mechanize->add_header(
"Connection" => "keep-alive",
"Keep-Alive" => "115");
$mechanize->get( $uri );
$mechanize->success or die "Could not fetch login page.\n";
#One of many different forms I've tried
$mechanize->form_name('login');
$mechanize->set_visible($user, $pass);
$mechanize->click();
$mechanize->follow_link();
$mechanize->success or die 'Could not login.';
print "Logged in successfully! Trying to look at TF2WH now.\n";
#Test to see if we get the "You need to login" message
$mechanize->get('http://www.tf2wh.com/item.php?id=6011;6;78e2c5962db56a60f7c143a12875f3b6');
print "Fetched\n";
if($mechanize->text() =~ m{Handy Hint:})
{
print "Failed to login.\n";
}
else
{
print "Up and running!";
}