简体   繁体   English

WWW ::机械化和Cookies

[英]WWW::Mechanize and Cookies

I use WWW::Mechanize::Shell to test stuff. 我使用WWW :: Mechanize :: Shell来测试东西。 Since I didn't managed to sign in on a web site I want to scrape, I thought I will use the browser cookie (chrome or firefox) for that specific website with the 'cookie' command WWW::Mechanize::Shell has. 由于我没有设法在我想要抓取的网站上登录,我想我将使用'cookie'命令WWW :: Mechanize :: Shell的特定网站使用浏览器cookie(chrome或firefox)。

The question is, Cookies usually stored in a single file, which is not good, how to get a cookie for only this specific site? 问题是,Cookies通常存储在一个文件中,这不好,如何只为这个特定网站获取cookie?

thanks, 谢谢,

Why isn't storing cookies in a file good? 为什么不将cookie存储在文件中?

Since WWW::Mechanize is built on top of LWP::UserAgent , you handle cookies just like you do in LWP::UserAgent . 由于WWW ::机械化是建立在之上LWP :: UserAgent的 ,你处理就像你在做饼干LWP :: UserAgent的 You can make the cookie jar a file or an in-memory hash. 您可以将cookie jar设置为文件或内存中的哈希。

If you don't want to save the cookies in a file, use an empty hash reference when you construct the mech object: 如果您不想将cookie保存在文件中,请在构造mech对象时使用空哈希引用:

 use WWW::Mechanize;

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

If you want to use a new file, make a new HTTP::Cookies object: 如果要使用新文件,请创建一个新的HTTP :: Cookies对象:

 use WWW::Mechanize;

 my $mech = WWW::Mechanize->new( 
     cookie_jar => HTTP::Cookies->new( file => "$ENV{HOME}/.cookies.txt" ) 
     );

If you want to load a browser specific cookies file, use the right module for it: 如果要加载特定于浏览器的cookie文件,请使用正确的模块:

 use WWW::Mechanize;

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

If you want no cookies at all, use undef explicitly: 如果您根本不需要cookie,请明确使用undef:

 use WWW::Mechanize;

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

All of this is in the docs. 所有这些都在文档中。

HTTP::Cookies::NetscapeHTTP::Cookies::Microsoft加载您现有的浏览器cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM