2015-07-12 103 views
0

我有一個腳本,從網站上刮我的分數。本網站使用了我的Facebook賬戶登錄由於幾天的腳本不工作了,我得到了以下錯誤:Perl Facebook登錄與機械化失敗餅乾錯誤

Cookies Required: Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.

我搜索了這個論壇,我發現了一個類似的問題: 我tryed的給定的解決方案,但它沒有奏效。

我已經使用此插件從谷歌瀏覽器導出我的FaceBook登錄cookies.txt。 https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg?hl=en並在同一文件夾保存cookie.txt的作爲腳本:

use WWW::Mechanize; 
use use HTTP::Cookies::Netscape; 
my $mech = WWW::Mechanize->new(cookie_jar => HTTP::Cookies::Netscape->new(file => $cookies.txt)); 
$mech->agent_alias('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0'); 
$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;  

在我使用代理腳本:火狐但cookie是從Chrome的出口,這是一個可能的問題?我嘗試了不同的方法和語言Python和Perl。

+0

的http://計算器.com/questions/31283839/facebook-login-via-perl-wwwmechanize-login-error-cookies-required – nexoma

+0

是的,我已經閱讀過這個話題,我通過導出cookies.txt來嘗試解決方案。不幸的是,我收到了同樣的錯誤:您的瀏覽器沒有啓用Cookie。 –

回答

2

Always use strict and use warnings at the top of your script

在腳本此行是問題

my $mech = WWW::Mechanize->new(cookie_jar => HTTP::Cookies::Netscape->new(file => $cookies.txt));

將其更改爲

my $cookies= 'cookies.txt'; #your cookies file path here 
my $mech = WWW::Mechanize->new(cookie_jar => HTTP::Cookies::Netscape->new(file => $cookies)); 

改正所有錯誤的腳本應該是:

#!/usr/bin/perl 
use strict; 
use warnings; 
use WWW::Mechanize; 
use HTTP::Cookies::Netscape; #you were using two times use 
my $cookies='your cookies file path'; 
my $mech = WWW::Mechanize->new(cookie_jar => HTTP::Cookies::Netscape->new(file => $cookies)); 
$mech->agent_alias('Windows Mozilla'); #this is right user agent 
$mech->get("https://www.facebook.com/login.php"); 
$mech->submit_form(
fields => { 
    email =>'your username', 
    pass => 'your password', 
    } 
); 

open(my $out, ">", "output_page.html") or die "Can't open output_page.html: $!"; #make lexical variables using my 
print $out $mech->content; 

我會建議您閱讀WWW::Mechanize documentation

注意:許多其他用戶建議使用這種工具登錄類型是違反其ToS的。請檢查Facebook API

+0

這樣做'my $ cookies ='/ Users/Boy/Downloads/cookies.txt'' –

1

沒有必要,如果你加載正確的餅乾,因爲這些代碼對我的作品(你可以看到你只需要兩個cookiec_userxs)發送憑據:

my $config = { 

    auth_token => 
     "c_user=100009668980203;xs=143%3AfaO_5XiRk_rWOg%3A2%3A1435225463%3A-1", 
}; 

my @cookies = split /;/, $config->{auth_token}; 

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

foreach my $cookie (@cookies) { 

    my ($cookie_name, $cookie_value) = split /=/, $cookie, 2; 
    $mech->cookie_jar->set_cookie(0, $cookie_name, $cookie_value, '/', 
     '.facebook.com', undef, 1, 1, undef, 1); 
} 

$mech->get("https://www.facebook.com/messages/"); 

$mech->save_content("$Bin/Messages_test.htm", binmode => ':raw');