简体   繁体   English

在 Google 表格上使用 JSON 获取 URL

[英]FetchingURL Using JSON on Google Sheets

I am attempting to use JSON to extract followers on Spotify Playlists.我正在尝试使用 JSON 来提取 Spotify 播放列表上的关注者。 It seems like various playlists have different HTML data - which makes it complicated.似乎各种播放列表具有不同的 HTML 数据 - 这使得它变得复杂。

Here's the code I have currently in Google Apps Scripts.这是我目前在 Google Apps Scripts 中的代码。 (courtesy of @Tanaike) (@Tanaike 提供)

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url).getContentText();
  const v = res.replace(/&/g, "&").match(/Spotify\.Entity \=([\s\S\w]+?);/);
  return v && v.length == 2 ? JSON.parse(v[1].trim()).followers.total : "Value cannot be 
retrieved.";
}

Then I just used =SAMPLE(the link) in the cells然后我只是在单元格中使用了 =SAMPLE(the link)

However, I am getting this error on some playlist links (I don't know why): SyntaxError: Unexpected end of JSON input (line 4).但是,我在某些播放列表链接上收到此错误(我不知道为什么):SyntaxError: Unexpected end of JSON 输入(第 4 行)。

Examples of working links: https://open.spotify.com/playlist/5aSO2lT7sVPKut6F9L6IAc https://open.spotify.com/playlist/7qvQVDnLe4asawpZqYhKMQ工作链接示例: https://open.spotify.com/playlist/5aSO2lT7sVPKut6F9L6IAc https://open.spotify.com/playlist/7qvQKVDnLe4asawpZqYh

Examples of nonworking links (shows the error stated above) https://open.spotify.com/playlist/2Um96ZbAH1fFTHmAcpO50n https://open.spotify.com/playlist/68jtRFcWtaFNHKO5wYLBsk无效链接示例(显示上述错误) https://open.spotify.com/playlist/2Um96ZbAH1fFTHmAcpO50n https://open.spotify.com/playlist/68skjtRFcWTA

About 80% of the links work (I have over 400).大约 80% 的链接有效(我有超过 400 个)。 Any guidance or help would be greatly appreciated.任何指导或帮助将不胜感激。

Thank you everyone!谢谢大家!

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the number of followers from the following URLs.您想从以下 URL 检索关注者的数量。

     https://open.spotify.com/playlist/5aSO2lT7sVPKut6F9L6IAc https://open.spotify.com/playlist/7qvQVDnLe4asawpZqYhKMQ https://open.spotify.com/playlist/2Um96ZbAH1fFTHmAcpO50n https://open.spotify.com/playlist/68jtRFcWtaFNHKO5wYLBsk https://open.spotify.com/playlist/2phIYXF2hAd5gTj00IwXbU https://open.spotify.com/playlist/7MBKSFzpPLQ9ryPtydXVwf https://open.spotify.com/playlist/148My4xA7WoEaNDmnl2A8Z https://open.spotify.com/playlist/4zMaYNXz2pP1OPGz9SIJhX https://open.spotify.com/playlist/2hBohCkXzjzTLh6jtd7gUZ

Modification points:修改点:

  • I think that in the current stage, all JSON data is retrieved and the data is parsed, and then, the value is retrieved.我认为在当前阶段,检索所有 JSON 数据并解析数据,然后检索值。 In this case, I thought that the specific characters might be included and those characters might occur the issue.在这种情况下,我认为可能包含特定字符并且这些字符可能会出现问题。

So in this answer, I would like to retrieve the part of value from JSON data and parsed it and returned value.所以在这个答案中,我想从 JSON 数据中检索值的一部分并解析它并返回值。 The modified script is as follows.修改后的脚本如下。

Modified script:修改后的脚本:

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url).getContentText();
  const v = res.match(/followers":({[\s\S\w]+?})/);
  return v && v.length == 2 ? JSON.parse(v[1].trim()).total : "Value cannot be retrieved.";
}

Result:结果:

When above script is used for the above 9 URLs, the following result is obtained.当上述脚本用于上述 9 个 URL 时,会得到以下结果。

在此处输入图像描述

Note:笔记:

  • This sample script is for the URLs in your question.此示例脚本适用于您问题中的 URL。 So when you tested it for other URLs, the script might not be able to used.因此,当您针对其他 URL 进行测试时,该脚本可能无法使用。 And, when the structure of HTML is changed at the server side, the script might not be able to used.并且,在服务器端更改 HTML 的结构时,脚本可能无法使用。 So please be careful this.所以请注意这一点。

Updated on January 15, 2022: 2022 年 1 月 15 日更新:

It seems that the specification of the HTML data is a bit changed. HTML 数据的规范似乎有点改变。 So I updated the above script as follows.所以我更新了上面的脚本如下。

Sample script:示例脚本:

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url).getContentText();
  const v = res.match(/"followers":({[\s\S\w]+?})/);
  return v && v.length == 2 ? JSON.parse(v[1].trim()).total : "Value cannot be retrieved.";
}

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

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