You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nba_api批量获取球员数据时遭遇ReadTimeout错误的解决求助

解决nba_api批量请求球员数据时的ReadTimeout问题

问题重现

批量请求所有NBA球员生涯数据和奖项时触发ReadTimeout错误,单独查询单个球员功能正常,代码如下:

from nba_api.stats.static import players 
from nba_api.stats.endpoints import playercareerstats
from nba_api.stats.endpoints import PlayerAwards
all_players = players.get_players()

for i in all_players:
        Player_id = i["id"]
        Player_careerstats = playercareerstats.PlayerCareerStats(Player_id).career_totals_regular_season.get_data_frame()
        awards = PlayerAwards(Player_id).get_data_frames() 

错误信息:

ReadTimeout: HTTPSConnectionPool(host='stats.nba.com', port=443): Read timed out. (read timeout=30)

解决方案

1. 添加请求间隔延迟

批量高频请求会被stats.nba.com限流,每次请求后添加固定延迟降低请求频率:

from nba_api.stats.static import players 
from nba_api.stats.endpoints import playercareerstats
from nba_api.stats.endpoints import PlayerAwards
import time

all_players = players.get_players()

for i in all_players:
        Player_id = i["id"]
        # 添加2秒延迟,可根据实际情况调整
        time.sleep(2)
        Player_careerstats = playercareerstats.PlayerCareerStats(Player_id).career_totals_regular_season.get_data_frame()
        awards = PlayerAwards(Player_id).get_data_frames() 

2. 延长请求超时时间

默认超时30秒,部分球员数据加载慢时会触发超时,手动设置更长的超时参数:

from nba_api.stats.static import players 
from nba_api.stats.endpoints import playercareerstats
from nba_api.stats.endpoints import PlayerAwards
import time

all_players = players.get_players()

for i in all_players:
        Player_id = i["id"]
        time.sleep(2)
        # 设置超时为60秒
        Player_careerstats = playercareerstats.PlayerCareerStats(Player_id, timeout=60).career_totals_regular_season.get_data_frame()
        awards = PlayerAwards(Player_id, timeout=60).get_data_frames() 

3. 异常捕获与自动重试

针对超时异常做捕获,失败后自动重试,避免单次失败中断整个任务:

from nba_api.stats.static import players 
from nba_api.stats.endpoints import playercareerstats
from nba_api.stats.endpoints import PlayerAwards
import time
import requests

all_players = players.get_players()

for i in all_players:
        Player_id = i["id"]
        retry_count = 3  # 最多重试3次
        while retry_count > 0:
            try:
                time.sleep(2)
                Player_careerstats = playercareerstats.PlayerCareerStats(Player_id, timeout=60).career_totals_regular_season.get_data_frame()
                awards = PlayerAwards(Player_id, timeout=60).get_data_frames()
                break
            except requests.exceptions.ReadTimeout:
                retry_count -= 1
                print(f"请求球员ID {Player_id} 超时,剩余重试次数:{retry_count}")
                time.sleep(5)  # 重试前延长延迟
        if retry_count == 0:
            print(f"球员ID {Player_id} 多次请求失败,跳过")

4. 分批次处理球员

将球员列表拆分为多个小批次,每处理完一批次后休息更长时间,进一步降低请求压力:

from nba_api.stats.static import players 
from nba_api.stats.endpoints import playercareerstats
from nba_api.stats.endpoints import PlayerAwards
import time

all_players = players.get_players()
batch_size = 50  # 每批次处理50个球员

# 拆分批次
batches = [all_players[i:i+batch_size] for i in range(0, len(all_players), batch_size)]

for batch in batches:
        for i in batch:
                Player_id = i["id"]
                time.sleep(2)
                Player_careerstats = playercareerstats.PlayerCareerStats(Player_id, timeout=60).career_totals_regular_season.get_data_frame()
                awards = PlayerAwards(Player_id, timeout=60).get_data_frames()
        print("当前批次处理完成,休息10秒")
        time.sleep(10)

内容的提问来源于stack exchange,提问作者wnasi3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:42:54