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

调用NBA_API获取PlayerGameLogs时遭遇JSONDecodeError问题求助

解决NBA_API PlayerGameLogs接口JSONDecodeError问题

常见原因及修复方案

  • 参数格式或值错误
    这个错误通常意味着接口返回了非JSON内容(比如空响应、HTML错误页),首先检查核心参数:

    • 确认球员ID正确:Darius Garland的ID是1630163,不要用错ID值
    • 时间段参数date_from_nullable和date_to_nullable必须严格遵循YYYY-MM-DD格式,不能混用其他日期格式
      示例正确参数代码:
    from nba_api.stats.endpoints import playergamelogs
    
    game_logs = playergamelogs.PlayerGameLogs(
        player_id_nullable='1630163',
        date_from_nullable='2023-10-01',
        date_to_nullable='2024-04-10'
    )
    
  • 请求被限流或拦截
    NBA公共接口会限制请求频率,或因请求头不规范返回错误页。可以添加浏览器请求头模拟正常访问,或增加请求延迟:

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    }
    
    game_logs = playergamelogs.PlayerGameLogs(
        player_id_nullable='1630163',
        date_from_nullable='2023-10-01',
        date_to_nullable='2024-04-10',
        headers=headers
    )
    

    也可以手动打印响应内容,排查具体错误:

    response = game_logs.get_response()
    print(response.text)  # 查看返回的实际内容,判断是错误页还是空响应
    
  • 赛季参数不匹配
    如果指定时间段跨赛季,需确保赛季参数覆盖对应区间,或直接使用全赛季参数:

    from nba_api.stats.library.parameters import SeasonAll
    
    game_logs = playergamelogs.PlayerGameLogs(
        player_id_nullable='1630163',
        date_from_nullable='2023-10-01',
        date_to_nullable='2024-04-10',
        season_nullable=SeasonAll.all
    )
    
  • 依赖版本兼容问题
    即使已安装依赖,版本不兼容也可能导致异常,尝试更新到最新版:

    pip install --upgrade nba-api
    

调试步骤

  1. 打印请求URL,确认参数是否正确拼接:
    print(game_logs.url)
    
  2. 直接用浏览器访问该URL,查看返回内容是否为有效JSON。若为HTML错误页,说明参数错误或被拦截;若为空响应,大概率是请求频率过高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 15:10:16