python requests證書問題解決
阿新 • • 發佈:2020-01-09
用requests包請求https的網站時,我們偶爾會遇到證書問題。也就是常見的SSLerror,遇到這種問題莫慌莫慌。
這裡沒有找到合適的網站去報SSL證書的錯誤,所以就假裝請求了一個https的網站,然後給報了SSLerror了,然後下面是解決方法
可以直接關閉驗證ssl證書
import requests ''' :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param verify: (optional) Either a boolean,in which case it controls whether we verify the server's TLS certificate,or a string,in which case it must be a path to a CA bundle to use. Defaults to ``True``. ''' r = requests.get('https://kyfw.12306.cn',verify=False) print(r.text)
這種方式直接在函式裡面加如verify改變Ture或者False即可,因為post與get呼叫的都為request()函式,所以get與post都一樣。
如果這種方式奏效就用這種方式,如果不奏效就用下面的一種
import requests ''' :param verify: (optional) Either a boolean,in which case it must be a path to a CA bundle to use. Defaults to ``True``. ''' ## 證書路徑 cert = '../cert/test.pem' r = requests.get('https://kyfw.12306.cn',verify=cert) print(r.text)
就用這種,直接把證書的路徑丟給verify,請求即可
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。