【发布时间】:2021-11-09 01:04:37
【问题描述】:
我正在尝试使用 Google 脚本中的 cURL 从 this site (stats.nba.com) 获取数据作为 json 对象,以便我可以将其导入到 Google 表格中。
到目前为止,这是我的功能:
function IMPORTNBA() {
var url = "https://www.nba.com/stats/teams/opponent-shots-closest-defender/?Season=2021-22&SeasonType=Regular%20Season&CloseDefDistRange=6%2B%20Feet%20-%20Wide%20Open&DateFrom=10%2F01%2F2021&DateTo=11%2F08%2F2021"
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",
"If-Modified-Since": "Mon, 08 Nov 2021 22:57:01 GMT"
}
var response = UrlFetchApp.fetch(url, headers)
var data = JSON.parse(response.getContentText())
return data;
}
获取没有像我希望的那样执行,因为响应输出看起来不像 json,而是像这样(截断):
<!DOCTYPE html>
<html xmlns:ng="http://angularjs.org" lang="en" ng-app="stats" ng-strict-di stats-version="4.46.1" stats-build-date="2021-11-05 08:03 PM">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<title> Teams Opponent Shooting Dashboard Closest Defender | Stats | NBA.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A table featuring opponent shooting dashboard information for each team in the league based on selected filters." />
<link rel="canonical" href="https://www.nba.com/stats/teams/opponent-shots-closest-defender/?CloseDefDistRange=6%2B%20Feet%20-%20Wide%20Open&DateFrom=10%2F01%2F2021&DateTo=11%2F08%2F2021&Season=2021-22&SeasonType=Regular%20Season">
<base href="/stats/teams/opponent-shots-closest-defender/">
<meta property="og:url" content="http://www.nba.com/stats/teams/opponent-shots-closest-defender/?CloseDefDistRange=6%2B%20Feet%20-%20Wide%20Open&DateFrom=10%2F01%2F2021&DateTo=11%2F08%2F2021&Season=2021-22&SeasonType=Regular%20Season" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="NBA Stats" />
<meta property="og:title" content="Teams Opponent Shooting Dashboard Closest Defender" />
<meta property="og:description" content="A table featuring opponent shooting dashboard information for each team in the league based on selected filters." />
<meta property="og:image" content="/stats/media/img/touch/stats-icon-152x152.png" />
<meta property="og:sport" content="Basketball" />
<meta property="og:sports_league" content="NBA" />
<meta name="msapplication-tap-highlight" content="no">
<link rel="manifest" href="/stats/manifest.json">
<meta name="theme-color" content="#051c2e">
<meta name="mobile-web-app-capable" content="yes">
<meta name="application-name" content="NBA Stats">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="NBA Sta
我输入的标题不准确吗?
编辑:cURL 命令:
curl '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=' \
-H 'Connection: keep-alive' \
-H 'sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"' \
-H 'Accept: application/json, text/plain, */*' \
-H 'x-nba-stats-token: true' \
-H 'sec-ch-ua-mobile: ?0' \
-H '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' \
-H 'x-nba-stats-origin: stats' \
-H 'sec-ch-ua-platform: "Windows"' \
-H 'Origin: https://www.nba.com' \
-H 'Sec-Fetch-Site: same-site' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Referer: https://www.nba.com/' \
-H 'Accept-Language: en-US,en;q=0.9' \
-H 'If-Modified-Since: Mon, 08 Nov 2021 22:57:01 GMT' \
--compressed
【问题讨论】:
-
我认为您的 HTML 不是 JSON。所以我无法理解你的目标。可以问一下
The fetch isn't executing as I hoped because the response output looks not like json, but like this (truncated):的truncated的细节和你的期望值吗? -
截断意味着我没有粘贴整个 HTML,因为它只是为了表明响应是 HTML 而不是 json。当我执行一个 cURL 时,我得到一个 json 响应。
-
感谢您的回复。你能提供
When I execute a cURL, I get a json response.的curl命令吗? -
由于字符数有限,不适合此回复,但我更新了原始帖子以在底部显示 cURL。
-
感谢您的回复。不幸的是,当我测试修改后的脚本时,脚本出现超时错误。所以我担心这个网址不能从谷歌域访问。顺便说一句,在当前阶段,
User-Agent不能用 UrlFetchApp 更改。所以,我也很担心这个。很抱歉,我无法清楚地回答您的问题。这是因为我的技术不好。我对此深表歉意。如果您能原谅我的拙劣技能,我将不胜感激。
标签: json google-apps-script curl http-headers urlfetch