tags:

views:

73

answers:

1

Is there a way to print the cookies stored in a cookielib.CookieJar in a human-readable way?

I'm scraping a site and I'd like to know if the same cookies are set when I use my script as when I use the browser.

+1  A: 
import urllib2
from cookielib import CookieJar, DefaultCookiePolicy
policy = DefaultCookiePolicy(
rfc2965=True, strict_ns_domain=DefaultCookiePolicy.DomainStrict)
cj = CookieJar(policy)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://somewebsite.com")

[str(i) for i in cj]

Produces:

['<Cookie JSESSIONID=BE71BFC3EE6D9799DEBD939A7487BB08 for somewebsite.com>']
Mark
Ah, too obvious.. It didn't occur to me that the CookieJar was iterable.
Pär Bohrarper