1. ホーム
  2. python

urlで最大再試行回数を超えてしまう問題を解決しました

2022-02-10 19:37:41
<パス

エラーの内容

Requests.exceptions.ConnectionError: HTTPConnectionPool(host='baidu.com', port=80): 
Max retries exceeded with url: 
(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x2b09dfd00310>: 
Failed to establish a new connection:
 [Errno 110] Connection timed out',))


オリジナルコード

import requests

url = "http://baidu.com"
headers = {
    'Accept': "application/json, text/plain, */*",
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
ret = requests.get(url=url, headers=headers, timeout=300)


以下を修正します。

import requests

url = "http://baidu.com"
headers = {
    'Accept': "application/json, text/plain, */*",
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
requests.DEFAULT_RETRIES = 5 # Increase the number of retry connections
s = requests.session()
s.keep_alive = False # close redundant connections
ret = requests.get(url=url, headers=headers, timeout=300)