CookieJar类的子类:
CookieJar
:管理HTTP cookie值、存储HTTP请求生成的cookie、向传出的HTTP请求添加cookie的对象。整个cookie都存储在内存中,对CookieJar实例进行垃圾回收后cookie也将丢失。
FileCookieJar
:从CookieJar派生而来,用来创建FileCookieJar实例,检索cookie信息并将cookie存储到文件中。filename是存储cookie的文件名。delayload为True时支持延迟访问访问文件,即只有在需要时才读取文件或在文件中存储数据。
MozillaCookieJar
:从FileCookieJar派生而来,创建与Mozilla浏览器 cookies.txt兼容的FileCookieJar实例。
LWPCookieJar
:从FileCookieJar派生而来,创建与libwww-perl标准的 Set-Cookie3 文件格式兼容的FileCookieJar实例。
引入文件
# -*- coding: utf-8 -*-
# @File : cookiejar_demo.py
# @Date : 2018-07-24
# @Author : Peng Shiyu
from http import cookiejar
from urllib import request
import ssl
# 关闭证书验证, 避免urllib SSLError
ssl._create_default_https_context = ssl._create_unverified_context
def get_cookie():
# 创建cookiejar实例对象
cookie = cookiejar.CookieJar()
print(cookie)
#
# 创建管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
http_handler = request.HTTPHandler()
https_handler = request.HTTPSHandler()
# 创建请求求管理器
opener = request.build_opener(cookie_handler, http_handler, https_handler)
# 发起请求
url = "https://www.baidu.com"
req = request.Request(url)
response = opener.open(req)
# 查看请求结果
print(response.reason) # OK
print(cookie)
"""
,
,
]>
"""
def save_cookie():
# 创建cookiejar实例对象
cookie = cookiejar.MozillaCookieJar("cookie.txt")
print(cookie)
#
# 创建管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
http_handler = request.HTTPHandler()
https_handler = request.HTTPSHandler()
# 创建请求求管理器
opener = request.build_opener(cookie_handler, http_handler, https_handler)
url = "https://www.baidu.com"
req = request.Request(url)
# 发起请求
response = opener.open(req)
# 查看请求结果
print(response.reason) # OK
cookie.save()
cookie.txt文件如下
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
.baidu.com TRUE / FALSE 3679895840 BIDUPSID 3204019AEE348DAEBBD8E7420F13494E
.baidu.com TRUE / FALSE 3679895840 PSTM 1532412187
www.baidu.com FALSE / FALSE 1532412493 BD_NOT_HTTPS 1
按照cookie.txt的格式,修改自己的cookie文件,保存为httpbin-cookie.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
.httpbin.org TRUE / FALSE 3679895840 username pengshiyu
.httpbin.org TRUE / FALSE 3679895840 password 123456
读取
def load_cookie():
# 创建cookiejar实例对象
cookie = cookiejar.MozillaCookieJar()
cookie.load("httpbin-cookie.txt")
print(cookie)
# , ]>
# 创建管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
http_handler = request.HTTPHandler()
https_handler = request.HTTPSHandler()
# 创建请求求管理器
opener = request.build_opener(cookie_handler, http_handler, https_handler)
url = "http://www.httpbin.org/cookies"
req = request.Request(url)
# 发起请求
response = opener.open(req)
# 查看请求结果
print(response.reason) # OK
print(response.read())
# b'{"cookies":{"password":"123456","username":"pengshiyu"}}\n'
参考:
http.cookiejar库之CookieJar