[英]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
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.
修改后的脚本如下。
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.";
}
When above script is used for the above 9 URLs, the following result is obtained.当上述脚本用于上述 9 个 URL 时,会得到以下结果。
It seems that the specification of the HTML data is a bit changed. HTML 数据的规范似乎有点改变。 So I updated the above script as follows.
所以我更新了上面的脚本如下。
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.