Python爬蟲連載7-cookie的保存與讀取、SSL講解

1、cookie的保存與讀取html

1.cookie的保存-FileCookie.Jarpython

 

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":"1215217867@qq.com",

        "password":"481648541615485"

    }

    #把數據進行編碼

    data = parse.urlencode(data)

    #建立一個請求對象

    req = request.Request(url,data=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的讀取git

 

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":"1215217867@qq.com",

        "password":"481648541615485"

    }

    #把數據進行編碼

    data = parse.urlencode(data)

    #建立一個請求對象

    req = request.Request(url,data=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文件,而且​訪問網頁成功。github

2、SSLweb

1.什麼是SSL安全

(1)SSL證書就是指遵照SSL安全套階層協議的服務器數字證書(SercureSocketLayer)服務器

(2)該證書是由美國網景公司開發微信

(3)CA(CertifacateAuthority)是數字證書認證中心,是發放、管理、廢除數字證書的收信人的​第三方機構。cookie

(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)

 

3、源碼

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.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料

 

相關文章
相關標籤/搜索