Importing javascript table into Google Docs spread

2020-07-27 04:57发布

I am attempting to import the leaderboard at pgatour.com/leaderboard.html or golf.com/leaderboard (sorry, can't post more than 2 links since I have <10 reputation).

Based on my research, it seems that the IMPORTHTML & IMPORTXML functions are unable to retrieve the data because the table doesn't actually exist at the time the import function runs, as the table/data is loaded via javascript after the function reads the page's html/xml. Example:

=IMPORTXML("http://www.golf.com/leaderboard","//*[@id='leaderboardPositionTable']")
=IMPORTHTML("http://www.pgatour.com/leaderboard.html","table",1)

Is anyone aware of a way to pull such a table into a Google Docs spreadsheet? I tried to follow the advice here, but honestly don't have a clue if importing JSON is the right approach, or if I'm even doing it correctly.

Any help would be much appreciated. Thanks

1条回答
虎瘦雄心在
2楼-- · 2020-07-27 04:58

After looking into this, I think you should take the following approach. It turns out that leaderboard is available as a JSON file. I wrote a function to test out the import and it works well.

function update_leaderboard(){
  var url = "http://www.pgatour.com/data/r/033/leaderboard-v2.json?ts=" + new Date().getTime()
  var result = UrlFetchApp.fetch(url);
  var response = result.getContentText();
  var data = JSON.parse(response);
  var w = SpreadsheetApp.getActive();
  var s = w.getActiveSheet();
  s.clear();
  //Write the JSON to the spreadsheet...
}

Having gotten that far, the only thing left to do is write that information onto the spreadsheet.

查看更多
登录 后发表回答