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信息也保存下来

 

posted on 2019-06-26 17:49  wy0925  阅读(1176)  评论(0编辑  收藏  举报