简体   繁体   English

如何在Google Apps脚本中从在线数据库查询?

[英]How to query from an online database in Google Apps Scripts?

I am trying to use google apps scripts and google sheets to basically automatically update a google sheet with statistical information every time an NHL team plays. 我正在尝试使用Google Apps脚本和Google工作表来基本上在每次NHL团队参加比赛时自动使用统计信息更新Google工作表。 How do I query nhl.com online database within the Google Apps Scripts environment? 如何在Google Apps脚本环境中查询nhl.com在线数据库? For example -- a stat that I am interested in is total points for a certain team. 例如,我感兴趣的统计数据是特定团队的总积分。 How would I get that integer value from nhl.com in Google Apps Scripts? 如何在Google Apps脚本中从nhl.com获取该整数值?

You need to use UrlFetchApp that is the GAS's service to send http requests and accept responses. 您需要使用GAS的服务UrlFetchApp发送http请求并接受响应。

Your code might look something like: 您的代码可能类似于:

 function getMatchResults() {
    var options = {
        'method': 'get',
        //and any other additional parameters you might need
    };
    var url = 'http://www.nhl.com/stats/team?aggregate=0&gameType=2&report=teamsummary&teamId=24&reportType=season&seasonFrom=20172018&seasonTo=20172018&filter=gamesPlayed,gte,1&sort=points%22'
    var results = UrlFetchApp.fetch(url, options); 
    Logger.log(results);
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    //further on goes your code to actually save the results into your sheet
}

If you need this function to run periodically, you'll need to set up a time driven trigger which is described here . 如果您需要此功能,定期运行,你需要设置其描述时间驱动的触发位置

UPDATE: After your comment, there goes the second part. 更新:在您发表评论后,还有第二部分。 At first, I assumed that nhl.com has some kind of API that would return responses in JSON format but turns it does not. 最初,我假设nhl.com具有某种API,该API会以JSON格式返回响应,但事实并非如此。 This is why you were getting the error at JSON.parse — the returned result is not a JSON object but an HTML web page. 这就是为什么在JSON.parse处出现错误的原因-返回的结果不是JSON对象,而是HTML网页。

So your task gets trickier: you try scraping the page and get the results out of html or use third party APIs like MySportFeeds . 因此,您的任务变得更加棘手:您尝试抓取页面并从html中获取结果,或者使用MySportFeeds之类的第三方API。 You can also see this Quora discussion . 您也可以查看Quora讨论

If you use MySportFeeds, which seems to be free for personal use, the above function will probably look something like: 如果您使用MySportFeeds(似乎免费供个人使用),则上述功能可能类似于:

function getMatchResults() {
  var headers = {
    'Authorization': 'username:password',
  };
  var options = {
    'method': 'get',
    'headers': headers,
    //and any other additional parameters you might need
  };
  var url = 'https://www.mysportsfeeds.com/api/feed/pull/nfl/2016-2017-regular/scoreboard.json?fordate=20161211'
  var results = UrlFetchApp.fetch(url, options); 
  Logger.log(results);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //further on goes your code to actually save the results into your sheet
}

UPDATE #2: 更新#2:

As per one more comment :) You have 3 options actually: 每发表一则评论:)实际上,您有3个选择:

  • scrape the whole page (see wikipedia article about that and here ) 刮整个页面(有关内容,请参阅Wikipedia文章 此处
  • use some third party API (MySportFeeds above) 使用一些第三方API(上面的MySportFeeds)
  • look into page source and find where it loads its data from — see this answer for more detail. 查看页面源并查找从何处加载数据—有关更多详细信息,请参见此答案

Using the last option, the code look like: 使用最后一个选项,代码如下所示:

function getMatchResults() {
  var headers = {
    "Origin": "http://http://www.nhl.com/"
  };
  var options = {
    'method': 'get',
    //and any other additional parameters you might need
  };
  var baseUrl = 'http://www.nhl.com/stats/rest/individual/team/basic/game/teamsummary'
  var urlParameters = '?cayenneExp=teamId=23';
  var url = baseUrl + urlParameters;
  var results = UrlFetchApp.fetch(url, options); 
  Logger.log(results);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //further on goes your code to actually save the results into your sheet
}

You can see here that stats are returned by http://www.nhl.com/stats/rest/individual/team/basic/game/teamsummary?cayenneExp= and in the cayenneExp parameter you should specify you additional settings (like teamId=23 which is Vancouver in the example). 您可以在此处看到http://www.nhl.com/stats/rest/individual/team/basic/game/teamsummary?cayenneExp=返回的统计信息,并且在cayenneExp参数中应指定其他设置(例如teamId = 23(在示例中为温哥华)。 This is how it looks in devtools: http://take.ms/fSH7H 这是在devtools中的外观: http : //take.ms/fSH7H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM