解決報(bào)錯(cuò)requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘xxx’, port=443): Max re
解決報(bào)錯(cuò)requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘xxx’, port=443): Max retries exceeded with url 使用requests時(shí)出錯(cuò)
解決報(bào)錯(cuò)requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘xxx’, port=443): Max retries exceeded with url 使用requests時(shí)出錯(cuò)
一、先檢查一下自己的庫中是否裝了下面三個(gè)庫
certifi、cryptography、pyOpenSSL
# 沒有的話請依次安裝
pip install certifi
pip install cryptography
pip install pyOpenSSL
二、請?jiān)诰W(wǎng)頁請求中加入:verify=False
這個(gè)的含義是: requests設(shè)置移除SSL認(rèn)證
response = requests.get(url, headers=headers,verify=False)
print(response)
# 但是可能會出現(xiàn) InsecureRequestWarning 警告,
# 雖然不影響代碼采集但是看著不舒服,可以加上下面兩行:
import urllib3
urllib3.disable_warnings()
三、訪問頻繁
1、在代碼中加入延遲 time.sleep(3)
2、更換IP
3、更換使用隨機(jī)的UA
4、網(wǎng)絡(luò)問題,更換網(wǎng)絡(luò)或者使用手機(jī)流量
四、連接數(shù)過多
如果requests連接數(shù)很多,那么在請求中避免使用持久連接
可以在請求頭headers中加入
headers = {'Connection': 'close'}
requests.adapters.DEFAULT_RETRIES = 5
五、可能出現(xiàn)的別的原因
1、設(shè)置session
import requests
session = requests.session()
...
response = session.get(url, headers=headers,verify=False)
print(response)
2、requests請求網(wǎng)頁時(shí)可能會請求不到,重試,加入try…except…語句
try:
response = session.get(url, headers=headers,verify=False)
except:
response = session.get(url, headers=headers,verify=False)
print(response)

浙公網(wǎng)安備 33010602011771號