【发布时间】:2014-05-29 09:53:29
【问题描述】:
我可以遍历 HTTPS 响应的 cookie,但无法将它们保存到磁盘。为什么不呢?
cookies = cookielib.LWPCookieJar('mycookies.txt')
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
handlers = [
HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman),
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.HTTPCookieProcessor(cookies)
]
headers = {
/* headers go here */
}
# create and install the opener
opener = urllib2.build_opener(*handlers)
def fetch(uri):
req = urllib2.Request(uri, headers)
return opener.open(uri)
def dump():
for cookie in cookies:
print cookie.name, cookie.value
# retrieve the result
resp = fetch(url)
dump()
cookies.save()
当我运行代码时,响应中的 cookie 会打印到控制台,但是当我打开 mycookies.txt 文件时,我看到的只是 #LWP-Cookies-2.0
【问题讨论】: