tags:

views:

54

answers:

2

How can I print the values of the cookie/cookie_jar being set?

Trying:

##my $cookie_jar=HTTP::Cookies->new(file => "cookie.jar",autosave=>1,ignore_discard=>1);
my $cookie_jar=HTTP::Cookies->new(); ## Would like it to be in memory
my $agent = WWW::Mechanize->new(cookie_jar => $cookie_jar);

##my $agent = WWW::Mechanize->new();
##my $agent = WWW::Mechanize->new(autocheck => 1);

##$agent->cookie_jar( {} );

# we need cookies
##$agent->cookie_jar(HTTP::Cookies->new);

print "Set Cookie Jar?\n";
print $agent->cookie_jar->as_string();
print "\n";

$agent->get($url); // url is a https site

Not too much luck with any of these, what am I doing wrong?

+2  A: 

Well, you have to have some cookies in the cookie jar to see any cookies in the output. So far you have an empty cookie jar. Either ensure that you add some cookies or that the site you are accessing sets them:

use HTTP::Cookies;
use WWW::Mechanize;

my $cookie_jar = HTTP::Cookies->new;
my $agent      = WWW::Mechanize->new( cookie_jar => $cookie_jar );

$cookie_jar->set_cookie(
    qw(
    3
    cat
    buster
    /
    .example.com
    0
    0
    0
    )
    );

    $agent->get( 'http://www.amazon.com' );

print "Set Cookie Jar?\n", $agent->cookie_jar->as_string, "\n";

This gave me the output:

Set Cookie Jar?
Set-Cookie3: session-id=000-0000000-0000000; path="/"; domain=.amazon.com; path_spec; discard; version=0
Set-Cookie3: session-id-time=1272524400l; path="/"; domain=.amazon.com; path_spec; discard; version=0    Set-Cookie3: cat=buster; path="/"; domain=.example.com; port=0; version=3

However, you don't need to invoke HTTP::Cookies directly. LWP will take care of that. You just give cookie_jar a hash reference:

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

If you just want the cookies from a particular response, you can create a separate cookie jar to hold the ones you extract from the response:

use WWW::Mechanize;

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

my $response = $agent->get( 'http://www.amazon.com' );

my $cookie_jar = HTTP::Cookies->new;
$cookie_jar->extract_cookies( $response );

print $cookie_jar->as_string;
brian d foy
sry, new to this screen-scraping stuff. I have $agent->get($url); does this add the cookie to the cookie jar. Sorry I need to laugh after I typed that, LMAO!!!
Phill Pafford
Always show a complete working example so you don't waste anyone's time fixing the errors you don't actually have. :) You can edit your question to fix that, too. :)
brian d foy
hmm maybe I'm not asking the question right. How do I print the cookie that the site I'm crawling is sending/setting?
Phill Pafford
thnx again, I think I see how this works
Phill Pafford
+1  A: 

Your main problem seems to be that you are trying to print the cookies before you actually visit the site. Try moving your print statements after your call to get()

mpeters