thiiiiiking
thiiiiiking

Reputation: 1233

How can I get cookies using urllib2

I used urllib2 to crawl a website, I wanted to get the response cookies after I login. I tried this:

def test_login():
    log_data = {
        'account': my account,
        'password': my pwd
    }
    post_data = urllib.urlencode(log_data)
    cookjar = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookjar))
    headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0',
            'Host': the web host,
            'Referer': 
        }
    req = urllib2.Request(the login page, post_data, headers=headers)
    content = opener.open(req)
    print cookjar
    for item in cookjar:
        print item, item.value

I got this:

<cookielib.CookieJar[<Cookie PHPSESSID=aaaaa for the web/>, <Cookie ytkuser=bbbbb for the web>]>
<Cookie PHPSESSID=aaaaa for the web/> aaaaa 
<Cookie ytkuser=bbbbb for .yitiku.cn/> bbbbb

this is not the format I want, I want to get a dict like this: {'ytkuser':'','PHPSESSID':''}. How can I translate the data to a dict? Or any other way to get the cookies? Thanks

Upvotes: 2

Views: 2380

Answers (2)

thiiiiiking
thiiiiiking

Reputation: 1233

I got it, I use

item.name and item.value

Upvotes: 0

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52223

You can use comprehensions:

>>> data = dict((cookie.name, cookie.value) for cookie in cookjar)
>>> print data["PHPSESSID"]
"aaaaa"

Upvotes: 2

Related Questions