7

I use WWW::Mechanize::Shell to test stuff. 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.

The question is, Cookies usually stored in a single file, which is not good, how to get a cookie for only this specific site?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
snoofkin
  • 8,725
  • 14
  • 49
  • 86

2 Answers2

14

Why isn't storing cookies in a file good?

Since WWW::Mechanize is built on top of LWP::UserAgent, you handle cookies just like you do in LWP::UserAgent. You can make the cookie jar a file or an in-memory hash.

If you don't want to save the cookies in a file, use an empty hash reference when you construct the mech object:

 use WWW::Mechanize;

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

If you want to use a new file, make a new HTTP::Cookies object:

 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:

 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:

 use WWW::Mechanize;

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

All of this is in the docs.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Thats a good explanation, and was really helpful. But, actually what I want is to LOAD cookies that I already have, the cookies are in the firefox cookies file (no idea where is it...). Is it possible? – snoofkin Aug 28 '10 at 22:28
  • Well, use one of the HTTP::Cookie::* modules to handle the type of cookies file that you want to load. – brian d foy Aug 30 '10 at 21:59
5

HTTP::Cookies::Netscape, HTTP::Cookies::Microsoft load your existing browser cookies.

daxim
  • 39,270
  • 4
  • 65
  • 132