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

如何在Google Script中用cURL获取stats.nba.com JSON数据导入Google Sheet

解决方案

核心问题梳理

  • 请求地址错误:你当前使用的是NBA数据统计的前端展示页面URL,返回内容本身就是HTML网页。你提供的可用curl示例调用的是官方后端API接口https://stats.nba.com/stats/leaguedashoppptshot,这才是返回JSON格式数据的正确地址。
  • UrlFetchApp参数格式错误:Google Apps Script的UrlFetchApp.fetch()方法的第二个参数为总配置对象,请求头需要嵌套在headers属性下,不能直接传递headers对象。
  • 请求头细节偏差:你填写的sec-ch-ua-platform字段值缺少引号,和标准请求格式不匹配,另外过期的If-Modified-Since字段可能触发反爬校验,可以直接删除。

修正后可用代码

function IMPORTNBA() {
  // 替换为官方API接口地址,和你提供的curl请求地址一致
  var url = "https://stats.nba.com/stats/leaguedashoppptshot?CloseDefDistRange=6%2B+Feet+-+Wide+Open&College=&Conference=&Country=&DateFrom=10%2F01%2F2021&DateTo=11%2F08%2F2021&Division=&DraftPick=&DraftYear=&DribbleRange=&GameScope=&GameSegment=&GeneralRange=&Height=&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&ShotDistRange=&StarterBench=&TeamID=0&TouchTimeRange=&VsConference=&VsDivision=&Weight="
  var headers = {
    "Connection": "keep-alive",
    "sec-ch-ua": "\"Google Chrome\";v=\"95\", \"Chromium\";v=\"95\", \";Not A Brand\";v=\"99\"",
    "Accept": "application/json, text/plain, */*",
    "x-nba-stats-token": "true",
    "sec-ch-ua-mobile": "?0",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
    "x-nba-stats-origin": "stats",
    "sec-ch-ua-platform": "\"Windows\"",
    "Origin": "https://www.nba.com",
    "Sec-Fetch-Site": "same-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://www.nba.com/",
    "Accept-Language": "en-US,en;q=0.9"
  }
  // 修正参数格式,把headers放到配置对象的对应字段下
  var options = {
    'method': 'get',
    'headers': headers,
    'muteHttpExceptions': true // 可选,出错时返回详细错误信息方便调试
  }
  var response = UrlFetchApp.fetch(url, options)
  var data = JSON.parse(response.getContentText())
  
  // 若需要直接导入Google Sheet,可将返回的数据集转为二维数组返回,示例如下:
  var resultSet = data.resultSets[0]
  var output = [resultSet.headers] // 首行插入表头
  output = output.concat(resultSet.rowSet) // 插入每行数据
  return output
}

额外说明

如果运行时出现403类报错,可更新请求头中的User-Agent字段为你当前浏览器的真实UA,避免被反爬策略拦截。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 12:24:02