1. 程式人生 > >python使用requests時報錯requests.exceptions.SSLError: HTTPSConnectionPool

python使用requests時報錯requests.exceptions.SSLError: HTTPSConnectionPool

audio 獲取 是我 The href pid ofo erro sessions

報錯信息

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 72, in get
return request(get, url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py
", line 58, in request return session.request(method=method, url=url, **kwargs) File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 508, in request resp = self.send(prep, **send_kwargs) File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py
", line 640, in send history = [resp for resp in gen] if allow_redirects else [] File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 218, in resolve_redirects **adapter_kwargs File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 618, in
send r = adapter.send(request, **kwargs) File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\adapters.py", line 506, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host=www.baidu.com, port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, u[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)),))

過程

測試1

不指定headers時GET:

>>> import requests
>>> requests.get(‘http://www.baidu.com/‘)
<Response [200]>
>>> requests.get(‘http://www.baidu.com/‘)
<Response [200]>
>>> requests.get(‘http://www.baidu.com/‘)
<Response [200]>
>>> header = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1‘,}
>>> requests.get(‘http://www.baidu.com/‘, headers = header)
<Response [200]>

  


技術分享圖片

測試2

當指定headers的User-Agent為火狐瀏覽器時:

>>> header = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1‘,}
>>> requests.get(‘http://www.baidu.com/‘, headers = header)
<Response [200]>
>>> requests.get(‘http://www.baidu.com/‘, headers = header)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 72, in get
return request(‘get‘, url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 640, in send
history = [resp for resp in gen] if allow_redirects else []
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 218, in resolve_redirects
**adapter_kwargs
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host=‘www.baidu.com‘, port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, u‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)‘),))

  


技術分享圖片

分析

現象:第一次GET時正常,第二次GET時,會報錯.

不同點:User-Agent不相同

分析:由於報錯SSL證書驗證失敗,所以這次的訪問應該是https協議.但是我們明明使用的是http,所以,猜測訪問該網站後,被重定向到了https://www.baidu.com/

驗證

首先,進行GET時,關閉證書驗證.因為,如果不關閉,請求總是失敗,不能獲取到重定向的信息.

>>> response = requests.get(‘http://www.baidu.com/‘, headers = header, verify=False)
D:\python\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
>>> response.history
[<Response [302]>]
>>> response.url
u‘https://www.baidu.com/‘

 

技術分享圖片

當不指定User-Agent時

>>> response = requests.get(‘http://www.baidu.com/‘, verify=False)
>>> response.history
[]
>>> response.url
u‘http://www.baidu.com/‘

技術分享圖片

結論

當指定headers的User-Agent時,baidu的服務器會重定向到https的網址.因此報出SSL驗證失敗的錯誤.

解決方法

方法1:

在進行GET時,指定SSL證書.詳情見附件

方法2:

關閉證書驗證. 詳情見附件

附件

[各瀏覽器的User-Agent] http://www.useragentstring.com/pages/useragentstring.php

[SSL 證書驗證] http://docs.python-requests.org/zh_CN/latest/user/advanced.html#ssl

轉自:https://blog.csdn.net/win_turn/article/details/77142100

python使用requests時報錯requests.exceptions.SSLError: HTTPSConnectionPool