import urllib.request from http import cookiejar filename = 'cookie.txt' # 创建MoillaCookiejar对象 cookie = cookiejar.MozillaCookieJar() # 读取cookie变量 cookie.load(filename) # HttpCookieProcessor创建cookie处理器 handler = urllib.request.HTTPCookieProcessor(cookie) # 创建opener opener = urllib.request.build_opener(handler) headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36' } url = urllib.request.Request('https://movie.douban.com/',headers=headers) res = opener.open(url) print(cookie)
和保存十分相似。
一个是.load() 一个是.save()