导入XML以获取NFL分数2018-新XML?

时间:2018-09-06 23:20:12

标签: php xml google-sheets feed

去年,我获得了IMPORTXML,因此我可以实时获得Google表格的得分更新。我今天使用它为2018年做准备,但它仍在提取去年的数据。我不知道如何获得今年所需的信息,如果信息已更改或过早。我希望有更多知识的人可以引导我或将e指向正确的方向?

=importxml("http://www.nfl.com/liveupdate/scorestrip/ss.xml","//g/@eid" )

这是我曾经使用过的领域之一,至今仍遥遥领先于2017年。谢谢!

3 个答案:

答案 0 :(得分:5)

尝试一下:

http://www.nfl.com/ajax/scorestrip?season=2018&seasonType=REG&week=1

看起来您必须为season,type和week指定querystring参数,但看起来输出可能是相同的。

答案 1 :(得分:2)

看起来XML服务已终止或不那么容易找到。

但是我相信这个站点是JSON等效的。

http://www.nfl.com/liveupdate/scores/scores.json

该站点还提供实时数据,您可以按日期进行选择。

http://www.nfl.com/liveupdate/game-center/2018090600/2018090600_gtd.json

盯着第一个链接的输出之后,看起来您可以从中进行比赛并从中进行安排,然后在第二个中使用引用为整个比赛细节选择一个特定的比赛链接。

以下是一项功能,您可以使用http://www.nfl.com/liveupdate/scores/scores.json将结果导入Google表格并显示选定的字段。

要安装和使用它,请阅读此处的说明: https://developers.google.com/apps-script/guides/sheets/functions#using_a_custom_function

在工作表中选择一个单元格,然后输入= ReadNFLResults()

要定期(例如每2分钟运行一次)运行脚本,请遵循以下说明https://www.quora.com/How-can-I-periodically-run-a-Google-Script-on-a-Spreadsheet

/** Imports NFL results to your spreadsheet Ex: ReadNFLResults()
* @customfunction
*/

function ReadNFLResults(){

  try{

    var res = UrlFetchApp.fetch("http://www.nfl.com/liveupdate/scores/scores.json");
    var content = res.getContentText();
    var json = JSON.parse(content);   

    // Extracts the keys (these change every week)
    var keys = [];
    for(var k in json) keys.push(k);

    // Declare array for results
    var NFLResults =[];

    // Make a Title Row
    NFLResults.push(["Date","Stadium","Time","Qtr","Home Team","Hs1","Hs2","Hs3","Hs4","HsT","Vis Team","As1","As2","As3","As4","AsT"]);

    // Extracts the Games one per line
    for(n=0; n<keys.length; n++){
      var Date = ""+keys[n];
      var Stadium = json[keys[n]]["stadium"];
      var Clock = json[keys[n]]["clock"];
      var TV = json[keys[n]]["media"]["tv"];
      var Qtr = json[keys[n]]["qtr"];
      var Home = json[keys[n]]["home"]["abbr"];
      var Hs1 = json[keys[n]]["home"]["score"]["1"];
      var Hs2 = json[keys[n]]["home"]["score"]["2"];
      var Hs3 = json[keys[n]]["home"]["score"]["3"];
      var Hs4 = json[keys[n]]["home"]["score"]["4"];
      var HsT = json[keys[n]]["home"]["score"]["T"];
      var Away = json[keys[n]]["away"]["abbr"];
      var As1 = json[keys[n]]["away"]["score"]["1"];
      var As2 = json[keys[n]]["away"]["score"]["2"];
      var As3 = json[keys[n]]["away"]["score"]["3"];
      var As4 = json[keys[n]]["away"]["score"]["4"];
      var AsT = json[keys[n]]["away"]["score"]["T"];
      NFLResults.push([Date,Stadium,Clock,Qtr,Home,Hs1,Hs2,Hs3,Hs4,HsT,Away,As1,As2,As3,As4,AsT]);
    }

   // Return array to Google-Sheets
    return (NFLResults);

  }
  catch(err){
      return "Error getting data";  
  }

}

以下是截至2018年9月10日的示例结果:

NFL results in progress

答案 2 :(得分:0)

这是我的剧本。它运作良好。不要忘记将脚本触发器设置为每分钟或您希望的频率自动运行它。

function GetNFLScore(){

var url = ('http://www.nfl.com/liveupdate/scores/scores.json');
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);   
var bye = "";

// Extracts the keys (these change every week)
var keys = [];
for(var k in json) keys.push(k);

// Declare array for results
var NFLResults =[];

// Make a Title Row
NFLResults.push(["Qtr","Time","Home","Pts","Away","Pts","Pos.","Down","To Go","Yard Line","Note","Network"]);

// Extracts the Games one per line
for(n=0; n<keys.length; n++)  {   
  var Clock = json[keys[n]]["clock"];                 // Kickoff time if pregame, Game Clock if game time
  var Qtr = json[keys[n]]["qtr"];                     // Gamestate (Pre, or Final), Game quater if game time
  var Home = json[keys[n]]["home"]["abbr"];           // Home Team
  var HsT = json[keys[n]]["home"]["score"]["T"];      // Home Total Score
  var Away = json[keys[n]]["away"]["abbr"];           // Away Team
  var AsT = json[keys[n]]["away"]["score"]["T"];      // Away Total Score
  var TV = json[keys[n]]["media"]["tv"];              // TV Network (NBC, FOX, ESPN, ect..)
  var YardLine = json[keys[n]]["yl"];                 // Current scrimage yard line
  var PosTeam = json[keys[n]]["posteam"];             // Team with ball possession
  var RedZone = json[keys[n]]["redzone"];             // Redzone boolean
  var Down = json[keys[n]]["down"];                   // Current Down
  var ToGo = json[keys[n]]["togo"];                   // Current To Go yardage for 1st down
  var Note = json[keys[n]]["note"];                   // Scoring play (Touchdown, Field Goal, Extra Point, Safety)

  // Correcting team abbr
  if( Home == "JAC" ){ Home = "JAX";}                 // JAC > JAX
  else if( Home == "WAS" ){ Home = "WSH";}            // WAS > WSH
  else if( Home == "LA"  ){ Home = "LAR";}            // LA > LAR

  if( Away == "LA"  ){ Away = "LAR";}      
  else if( Away == "WAS" ){ Away = "WSH";}
  else if( Away == "JAC" ){ Away = "JAC";}

  // Clear non-score related game data if game is final
  if( Qtr == "Final" ){ 
    Clock = "";
    PosTeam = "";
    RedZone = "";
    Down = "";
    ToGo = "";
    Note = "";
    TV = "";
  } 
  else if( Qtr == "Final OT" ){ 
    Clock = "";
    PosTeam = "";
    RedZone = "";
    Down = "";
    ToGo = "";
    Note = "";
    TV = "";
  }

  // Display Game data
  NFLResults.push([Qtr,Clock,Home,HsT,Away,AsT,PosTeam,Down,ToGo,YardLine,Note,TV]);
}

// Clear the bottom of the scoreboard from lack of scores caused by bye weeks
var rows = NFLResults.length
for(n=0; n<(17-rows); n++) {
  NFLResults.push([bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye]);   // N'SYNC
}

// Create new tab 'NFL Scores' if it doesn't exist.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var itt = ss.getSheetByName('NFL Scores');
if (!itt) {

    ss.insertSheet('NFL Scores');
}

// Push Scoreboard to spreadsheet
SpreadsheetApp.getActive().getSheetByName('NFL Scores').getRange(1,1,17,12).setValues(NFLResults);

}