如何使用 urllib2 获取 cookie

How can I get cookies using urllib2

我使用 urllib2 抓取了一个网站,我想在登录后获取响应 cookie。我试过这个:

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

我知道了:

<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

这不是我想要的格式,我想要这样的字典: {'ytkuser':'','PHPSESSID':''}。如何将数据翻译成字典?或者有其他方式获取cookies吗?谢谢

我知道了,我用

item.name and item.value

您可以使用 comprehensions:

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