一、cookie的保存與讀取
1.cookie的保存-FileCookie.Jar
from urllib import request,parse from http import cookiejar #創建cookiejar實體 filename = "cookie.txt" cookie = cookiejar.MozillaCookieJar(filename) #生成cookie的管理器 cookie_handler = request.HTTPCookieProcessor(cookie) #創建http請求管理器 http_handler = request.HTTPHandler() #生成https管理器 https_handler = request.HTTPHandler() #創建請求管理器 opener = request.build_opener(http_handler,https_handler,cookie_handler) ? def login(): """ 負責初次登錄 需要輸入用戶名密碼 :return: """ url = "http://www.renren.com/PLogin.do" data = { "email":"[email protected]", "password":"481648541615485" } #把資料進行編碼 data = parse.urlencode(data) #創建一個請求物件 req = request.Request(url,data=https://www.cnblogs.com/ruigege0000/p/data.encode()) #使用opener發起請求 rep = opener.open(req) #保存cookie到檔案 #ignore_discard表示及時cookie將要被丟棄也要保存下來 #ignore_expire表示如果該檔案中cookie即使已經過期,保存 cookie.save(ignore_discard=True,ignore_expires=True) ? def getHomePage(): url = "http://www.renren.com/965187997/profile" #如果已經執行了login函式,則opener自動已經包含相應的cookie值 rsp = opener.open(url) ? html = rsp.read().decode() with open("rsp.html","w") as f: f.write(html) ? if __name__ == "__main__": """ 執行完login之后,會得到授權之后的cookie 我們嘗試把cookie列印出來 """ login() getHomePage()

2.cookie的讀取
from urllib import request,parse from http import cookiejar #創建cookiejar實體 cookie = cookiejar.MozillaCookieJar() cookie.load("cookie.txt",ignore_discard=True,ignore_expires=True) ? #生成cookie的管理器 cookie_handler = request.HTTPCookieProcessor(cookie) #創建http請求管理器 http_handler = request.HTTPHandler() #生成https管理器 https_handler = request.HTTPHandler() #創建請求管理器 opener = request.build_opener(http_handler,https_handler,cookie_handler) ? def login(): """ 負責初次登錄 需要輸入用戶名密碼 :return: """ url = "http://www.renren.com/PLogin.do" data = { "email":"[email protected]", "password":"481648541615485" } #把資料進行編碼 data = parse.urlencode(data) #創建一個請求物件 req = request.Request(url,data=https://www.cnblogs.com/ruigege0000/p/data.encode()) #使用opener發起請求 rep = opener.open(req) #保存cookie到檔案 #ignore_discard表示及時cookie將要被丟棄也要保存下來 #ignore_expire表示如果該檔案中cookie即使已經過期,保存 cookie.save(ignore_discard=True,ignore_expires=True) ? def getHomePage(): url = "http://www.renren.com/965187997/profile" #如果已經執行了login函式,則opener自動已經包含相應的cookie值 rsp = opener.open(url) ? html = rsp.read().decode() with open("rsp.html","w") as f: f.write(html) ? ? if __name__ == "__main__": """ 執行完login之后,會得到授權之后的cookie 我們嘗試把cookie列印出來 """ # login() getHomePage()
改代碼讀取了保存的cookie檔案,并且?訪問網頁成功,
二、SSL
1.什么是SSL
(1)SSL證書就是指遵守SSL安全套階層協議的服務器數字證書(SercureSocketLayer)
(2)該證書是由美國網景公司開發
(3)CA(CertifacateAuthority)是數字證書認證中心,是發放、管理、廢除數字證書的收信人的?第三方機構,
(4)遇到不信任的SSL證書,可以用代碼進行忽略掉
from urllib import request #匯入python ssl處理模塊 import ssl #利用非認證背景關系環境替換認證的下文環境 ssl._create_default_https_context = ssl._create_unverified_context url = "https://www.12306.cn/mormhweb/" rsp = request.urlopen(url) ? html = rsp.read().decode() ? print(html)

三、原始碼
Reptitle7_1_SaveCookie.py
Reptitle7_2_LoadCookie.py
Reptitle7_3_SSLAnalysis.py
https://github.com/ruigege66/PythonReptile/blob/master/Reptitle7_1_SaveCookie.py
https://github.com/ruigege66/PythonReptile/blob/master/Reptitle7_2_LoadCookie.py
https://github.com/ruigege66/PythonReptile/blob/master/Reptitle7_3_SSLAnalysis.py
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關注微信公眾號:傅里葉變換,個人公眾號,僅用于學習交流,后臺回復”禮包“,獲取大資料學習資料

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/196571.html
標籤:Python
下一篇:實用代碼,批量下載手機壁紙!
