swar / nba_api

An API Client package to access the APIs for NBA.com
MIT License
2.34k stars 515 forks source link

[Bug]: HTTPSConnectionPool(host='stats.nba.com', port=443): Read timed out. (read timeout=30) #405

Open ghadfield32 opened 7 months ago

ghadfield32 commented 7 months ago

NBA API Version

latest

Issue

I'm having the same kind of issue as described in this post for leaguegamefinder and playergamelog. Can you tell me where I'm going wrong in my api call? It's been working for NBA season so far as I've been running some models on this data until now. Just today it started giving me the timeout error.

Code

from nba_api.stats.endpoints import leaguegamefinder import pandas as pd import time import requests

Custom headers to mimic a browser request

custom_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Referer': 'https://stats.nba.com', 'Connection': 'keep-alive', }

def get_games_by_season(season_year_start, retries=3, delay=30): season_id = f"{season_year_start}-{str(season_year_start+1)[-2:]}"

for attempt in range(retries):
    try:
        gamefinder = leaguegamefinder.LeagueGameFinder(season_nullable=season_id)
        gamefinder.nba_api.client.headers = custom_headers
        gamefinder.nba_api.client.timeout = 60  # Reduced timeout

        games = gamefinder.get_data_frames()[0]
        return games
    except requests.exceptions.Timeout:
        print(f"Timeout for season {season_id}, retry {attempt+1}/{retries}. Waiting {delay} seconds...")
        time.sleep(600)  # Shorter delay before retrying
    except Exception as e:
        print(f"Error for season {season_id}: {e}")
        break

return pd.DataFrame()  # Empty DataFrame on failure

all_seasons_data = pd.DataFrame()

for year in range(2023, 2024): season_data = get_games_by_season(year) if not season_data.empty: all_seasons_data = pd.concat([all_seasons_data, season_data], ignore_index=True) else: print(f"No data for season starting in {year}")

LeKD6 commented 6 months ago

Did you ever find a solution? Having the same issue myself, randomly stopped working

SrijithVenkat commented 4 months ago

Having this same issue as well

erik-rt commented 1 month ago

+1 on this issue, anyone find a solution?

benj1229 commented 6 days ago

Same issue here

File "d:\batch\tasks\workitems\adfv2-bapredictordev01pool\job-1\94838f92-d599-4fed-9e90-c1097d4d2e5a\wd\main.py", line 75, in Main if get_query(leaguegamefinder.LeagueGameFinder().get_data_frames()[0]) is None: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\nba_api\stats\endpoints\leaguegamefinder.py", line 249, in __init__ self.get_request() File "C:\Python312\Lib\site-packages\nba_api\stats\endpoints\leaguegamefinder.py", line 252, in get_request self.nba_response = NBAStatsHTTP().send_api_request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\nba_api\library\http.py", line 146, in send_api_request response = requests.get( ^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\requests\api.py", line 73, in get return request("get", url, params=params, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\requests\adapters.py", line 501, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))