如何使用urllib2获取cookie

时间:2016-03-09 02:37:29

标签: python cookies 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

这不是我想要的格式,我想得到这样的字典: {&#39; ytkuser&#39;:&#39;&#39;&#39; PHPSESSID&#39;:&#39;&#39;}。如何将数据翻译成字典?或者任何其他方式来获取cookie?感谢

2 个答案:

答案 0 :(得分:2)

您可以使用comprehensions

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

答案 1 :(得分:0)

我明白了,我用

item.name and item.value