from urllib import request from http.cookiejar import MozillaCookieJar cookiejar = MozillaCookieJar('cookie.txt') handler = request.HTTPCookieProcessor(cookiejar) opener = request.build_opener(handler) resp = opener.open('http://www.baidu.com') cookiejar.save()
过期的cookie也能获取到
from urllib import request from http.cookiejar import MozillaCookieJar cookiejar = MozillaCookieJar('cookie.txt') cookiejar.load(ignore_discard=True) handler = request.HTTPCookieProcessor(cookiejar) opener = request.build_opener(handler) resp = opener.open('http://httpbin.org/cookies/set?course=abc') for c in cookiejar: print(c) # cookiejar.save(ignore_discard=True) # 过期的cookie信息也保存下来