我想从我的脚本中处理多次登录,所以我试图保存来自 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.
您正在对字符串而不是文件使用加载。 因为没有 as_string 的反向方法,所以您必须保存和加载,而不是 as_string 和加载。 您可能会尝试只使用多个 cookie jar ,例如
$old_cookies = $agent->cookie_jar;
$agent->cookie_jar({}); # new cookie jar
...
$agent->cookie_jar($old_cookies); # switch back to old cookie jar
关于Perl WWW::Mechanize 将 cookie 存储为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25347524/