如何通过 Google 表格上的应用程序脚本从 Youtube API 抓取 subscriberCount?

How do I scrape the subscriberCount from Youtube API through apps script on Google Sheets?

这是我目前拥有的:

function YoutubeScraper() {
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
  var activeSheet = spreadSheet.getActiveSheet()
  var search = YouTube.Search.list("snippet, id", {q: "pizza", maxResults: 50})
  var results = search.items.map((item) => [item.id.videoId, item.snippet.title, item.snippet.publishedAt, item.snippet.thumbnails.default.url])
  var ids = results.map((id) => id[0]).join(",")
  var stats = YouTube.Videos.list("statistics", {id: ids})
  var videoStats = stats.items.map((item) => [item.statistics.viewCount, item.statistics.likeCount, item.statistics.dislikeCount])
  activeSheet.getRange(2,1,results.length, results[0].length).setValues(results)
  activeSheet.getRange(2,3, videoStats.length, results[0].length).setValues(videoStats)
}

我尝试在

中添加item.statistics.subscriberCount
var videoStats = stats.items.map((item) => [item.statistics.viewCount, item.statistics.likeCount, item.statistics.dislikeCount])

但它不起作用。 https://developers.google.com/youtube/v3/docs/channels/list#common-use-cases 上有一条注释,指出“statistics.subscriberCount 属性 值已更新,以反映影响订阅者数量显示方式的 YouTube 政策变更”。我该怎么做才能获得 subscriberCount?输出应该是一个数据

修改点:

  • 我认为在您的情况下,为了检索 subscriberCount 的值,将像 YouTube.Channels.list 一样使用“频道:列表”的方法。另外,你提供的官方文档URL是针对“Channels: list”的方法。
  • 但是在您的脚本中,使用了 YouTube.Videos.list。我认为这可能是您遇到问题的原因。
  • 在您的脚本中,我认为您想将 subscriberCount 的值放在频道 ID 的同一行中。但是,在您当前的脚本中,results 有 4 列。但是 videoStats 是从“C”列中输入的。在这种情况下,列“C”和“D”被覆盖。

当以上几点反映到你的脚本中,就会变成下面这样。

修改后的脚本:

function YoutubeScraper() {
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
  var activeSheet = spreadSheet.getActiveSheet();
  var search = YouTube.Search.list("snippet", { q: "pizza", maxResults: 50 });
  var results = search.items.map((item) => [item.snippet.channelId, item.snippet.title, item.snippet.publishedAt, item.snippet.thumbnails.default.url]);
  var ids = results.map((id) => id[0]).join(",");
  var stats = YouTube.Channels.list("statistics", { id: ids });
  var videoStats = stats.items.map((item) => [item.id, item.statistics.viewCount, item.statistics.subscriberCount]).reduce((o, [id, ...v]) => Object.assign(o, {[id]: v}));
  results = results.map(e => videoStats[e[0]] ? e.concat(videoStats[e[0]]) : e.concat(Array(2).fill("")));
  activeSheet.getRange(2, 1, results.length, results[0].length).setValues(results);
}
  • 在这个修改中,videoStats的值被转换为一个对象来搜索频道ID。并且,使用该对象,每个值都被推送到 results 的相应行。然后,将值放入活动 sheet.
  • YouTube.Channels.list,没有item.statistics.likeCount, item.statistics.dislikeCount的属性。

参考:

已添加:

根据您的以下回复,

that issue has not been completely resolved. What I am trying to do is to get all the information I can, from YouTube.Seach: channelId, videoId, title of the video, when it was published and the thumbnail. From YouTube.Videos: the number of likes, dislikes views and comments. From YouTube.Channels: the overall number of views of the channels and the number of subscribers How about the following modified script?

修改后的脚本:

function YoutubeScraper2() {
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
  var activeSheet = spreadSheet.getActiveSheet();

  // Retrieve YouTube.Search.list.
  var search = YouTube.Search.list("snippet", { q: "pizza", maxResults: 50 });
  var results = search.items.map((item) => [item.snippet.channelId, item.id.videoId, item.snippet.title, item.snippet.publishedAt, item.snippet.thumbnails.default.url]);
  
  // Retrieve channel IDs and video IDs.
  var {channelIds, videoIds} = results.reduce((o, [channelId, videoId]) => {
    o.channelIds.push(channelId);
    o.videoIds.push(videoId);
    return o;
  }, {channelIds: [], videoIds: []});

  // Retrieve YouTube.Videos.list.
  var videoList = YouTube.Videos.list("statistics", {id: videoIds.join(",")});
  var videoStats = videoList.items.map((item) => [item.id, item.statistics.viewCount, item.statistics.likeCount, item.statistics.dislikeCount]).reduce((o, [id, ...v]) => Object.assign(o, {[id]: v}), {});

  // Retrieve YouTube.Channels.list
  var channelList = YouTube.Channels.list("statistics", { id: channelIds.join(",") });
  var channelStats = channelList.items.map((item) => [item.id, item.statistics.viewCount, item.statistics.subscriberCount]).reduce((o, [id, ...v]) => Object.assign(o, {[id]: v}), {});

  // Create an array for putting values and put the values to Spreadsheet.
  results = results.map(e => channelStats[e[0]] ? e.concat(channelStats[e[0]]) : e.concat(Array(2).fill("")));
  results = results.map(e => videoStats[e[1]] ? e.concat(videoStats[e[1]]) : e.concat(Array(3).fill("")));
  var header = ["channelId", "videoId", "title", "publishedAt", "thumbnails", "viewCount", "subscriberCount", "viewCount", "likeCount", "dislikeCount"];
  results.unshift(header);
  activeSheet.getRange(1, 1, results.length, results[0].length).setValues(results);
}
  • 当上面的脚本是 运行 时,列 channelId, videoId, title, publishedAt, thumbnails, viewCount, subscriberCount, viewCount, likeCount, dislikeCount 被放入活动 sheet.
  • 如果不想放表头,请修改最后3行为activeSheet.getRange(2, 1, results.length, results[0].length).setValues(results);
  • 如果要排列列,请修改上面的脚本。

结果:

当上面的脚本为运行时,得到下面的Spreadsheet