设置cookie

import urllib.request
import http.cookiejar

#设置cookie对象
cookie = http.cookiejar.CookieJar()
#设置处理cookie对象
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open("https://www.baidu.com")
#查看cookie的类型
print(type(cookie))
for item in cookie:
    print(item.name, "=", item.value, item.domain)

cookie包括 name , value , domain 等字段, 为字典形式

打印结果为

原文地址:https://www.cnblogs.com/zijidefengge/p/13388651.html