Facebook通过Perl登录WWW :: Mechanize,登录错误“Cookies必需” [英] Facebook login via Perl WWW::Mechanize, login error "Cookies Required"
问题描述
Cookies必需
您的浏览器未启用Cookie。请启用浏览器偏好设置中的Cookie以继续。
从我的了解,WWW :: Mechanize功能自动Cookie - 换句话说,默认构造函数,机器人应该接受cookie。浏览器应启用Cookie。我不会看到这个屏幕,对不对?
我已经阅读并实验了WWW:Mechanize和HTTP :: Cookies,但无论我尝试(愚弄)饼干罐每一种方式),我不能超过这个cookies必需错误。
这是我的代码没有任何实验绒毛,非常简单。
使用WWW :: Mechanize;
我的$ mech = WWW :: Mechanize-> new();
$ mech-> get(https://www.facebook.com/login.php);
$ mech-> submit_form(
fields => {
email =>'< my email here>',
pass =>'<我的密码这里>',
}
);
打开($ out,>,output_page.html)或死无法打开output_page.html:$!;
print $ out $ mech-> content;
捕获使用浏览器登录的会话如果您使用Firefox,您可以使用 firebug ,并检查设置了哪些HTTP标头 - how-to 。在脚本中设置相同的标题,包括用户代理,检查是否设置相同的Cookie,最后可能会出现以下情况:
use strict;
使用警告;
使用WWW :: Mechanize;
使用HTTP :: Cookies;
我的$ cookie_jar = HTTP :: Cookies-> new(
hide_cookie2 => 1,
);
我的$ mech = WWW :: Mechanize-> new(
cookie_jar => $ cookie_jar,
);
$ mech-> agent_alias('Windows Mozilla');
$ mech-> add_header('Connection'=>'keep-alive') ;
$ mech-> add_header('Acc ept-Language'=>'en-GB,en; q = 0.5');
$ mech-> add_header('Accept-Encoding'=>'gzip,deflate');
$ mech-> add_header('Accept'=>
'text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8');
I have a Perl script that I run in Strawberry Perl in which I login to Facebook using WWW::Mechanize. For a short background, I use my script to automate an occasional "leaderboard" for a fun little game I invented to be played among my group of friends. I assign points based upon certain posts, and the objective is to earn the most points. Very simple. This script is complete, and it's worked for me in the past (I last ran it 4 days ago). However, I am unable to login to Facebook today. I dumped the content after the form submission to an HTML file, and it appears that I've returned back to the login screen with a red error box:
Cookies Required
Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.
From what I understand, WWW::Mechanize features automatic cookies -- in other words by the default constructor, the bot should be accepting cookies. Cookies should be enabled by the browser. I should not see this screen, right?
I've read and experimented with WWW:Mechanize and HTTP::Cookies, but no matter what I try (fooling around with the cookie jar every which way), I cannot get past this "cookies required" error.
This is my code without any experimental fluff, very simple.
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get("https://www.facebook.com/login.php");
$mech->submit_form(
fields => {
email => '<my email here>',
pass => '<my password here>',
}
);
open($out, ">", "output_page.html") or die "Can't open output_page.html: $!";
print $out $mech->content;
Capture a session of you logging in with your browser (if you're using firefox you can use firebug, and check what HTTP headers are set - how-to. Set the same headers in your script, including the user agent. Check if the same cookies are set. You may end up with something like the below:
use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(
hide_cookie2 => 1,
);
my $mech = WWW::Mechanize->new(
cookie_jar => $cookie_jar,
);
$mech->agent_alias('Windows Mozilla');
$mech->add_header( 'Connection' => 'keep-alive' );
$mech->add_header( 'Accept-Language' => 'en-GB,en;q=0.5' );
$mech->add_header( 'Accept-Encoding' => 'gzip, deflate' );
$mech->add_header( 'Accept' =>
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' );
这篇关于Facebook通过Perl登录WWW :: Mechanize,登录错误“Cookies必需”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!