我想处理我脚本中的多功能登录,所以我试图将WWW::Mechanize中的实际cookie保存为一个值(按预期工作),稍后再使用它们,这将不起作用,下面是我的代码:

use WWW::Mechanize;
$agent = WWW::Mechanize->new( cookie_jar => {} );
$agent->get('https://example.com/');

#save cookies to string
$cookies =  $agent->cookie_jar->as_string;
#clearing cookies
$agent->cookie_jar->clear;

#re-using the cookies (this wont work)
$agent->cookie_jar->load($cookies);

有什么想法吗?提前谢谢!

最佳答案

HTTP::Cookies的文档中:

   $cookie_jar->load
   $cookie_jar->load( $file )
       This method reads the cookies from the file and adds them to the $cookie_jar.  The file must be in the format written by the save() method.

您使用的是字符串加载,而不是文件加载。
因为没有反向方法可以作为字符串保存和加载,而不是作为字符串和加载。
你可以尝试使用多个饼干罐,例如。
$old_cookies = $agent->cookie_jar;
$agent->cookie_jar({}); # new cookie jar
...
$agent->cookie_jar($old_cookies); # switch back to old cookie jar

10-05 20:54
查看更多