用requests包請求https的網站時,我們偶爾會遇到證書問題。也就是常見的SSLerror,遇到這種問題莫慌莫慌。
這里沒有找到合適的網站去報SSL證書的錯誤,所以就假裝請求了一個https的網站,然后給報了SSLerror了,然后下面是解決方法
1、可以直接關閉驗證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 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``. ''' ## 證書路徑 cert = '../cert/test.pem' r = requests.get('https://kyfw.12306.cn',cert=cert) print(r.text)
就用這種,直接把證書的路徑丟給verify,請求即可