您好,登錄后才能下訂單哦!
如下所示:
#保存 cookie 到變量 import urllib.request import http.cookiejar cookie = http.cookiejar.CookieJar() handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open('http://flights.ctrip.com/') for item in cookie: print('%s = %s' % (item.name,item.value)) #保存 cookie 到文件 import urllib.request import http.cookiejar cookie_file = 'E:/mypy/cookie.txt' cookie = http.cookiejar.MozillaCookieJar(cookie_file) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) #response = opener.open('http://flights.ctrip.com/') request = urllib.request.Request('http://flights.ctrip.com/',headers={"Connection": "keep-alive"}) response = opener.open(request) cookie.save(ignore_discard=True, ignore_expires=True) for item in cookie: print('%s = %s' % (item.name,item.value)) #從文件中讀取 cookie 訪問 import urllib.request import http.cookiejar cookie_file = 'E:/mypy/cookie.txt' cookie = http.cookiejar.MozillaCookieJar() cookie.load(cookie_file, ignore_discard=True, ignore_expires=True) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) request = urllib.request.Request('http://flights.ctrip.com/') html = opener.open(request).read().decode('gbk') print(html)
以上這篇Python Cookie 讀取和保存方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。