如何通过频道名称从 youtube api 获取订阅者数量

Ron*_*mar 5 youtube youtube-api node.js

我正在尝试使用 youtube 的 api 使用 node.js 通过频道名称获取频道的订阅者数量。我怎么做?

Rub*_*epo 6

您可以使用官方 YouTube API Channels.list方法来获取统计信息,例如:subscriberCount、videoCount 和 viewCount。

\n
\n\n\n\n\n\n\n\n
从 YouTube 频道获取统计信息 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0Run in Fusebit
\n
\n
const youtube = googleClient.youtube(\'v3\');\nconst channelName = \'GoogleDevelopers\';\nconst channelsResponse = await youtube.channels.list({\n  part: \'id,statistics\',\n  forUsername: channelName,\n});\n\nif (channelsResponse.data.items && channelsResponse.data.items.length) {\n  const { statistics: { subscriberCount, videoCount, viewCount } } = channelsResponse.data.items[0];\n  console.log(`The channel ${channelName} has \xe2\x80\x8d\xe2\x80\x8d ${subscriberCount} subscribers,  ${videoCount} videos and  ${viewCount} views.`);\n} else {\n  console.log = `Channel not found: ${channelName}`;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

[更新]

\n

根据评论,这里是一个如何根据搜索词搜索频道的示例,然后您将使用频道 id 来获取统计信息,从前面的代码中,您将发送id \n而不是发送forUserName参数请参阅此处的搜索 API 文档

\n
  const channelsResponse = await youtube.search.list({\n    part: \'snippet\',\n    q: searchTerm,\n    maxResults: 10,\n    type: \'channel\',\n    order: \'viewCount\' // show more popular first\n  });\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是搜索中每个项目返回的响应对象

\n
{\n  "kind": "youtube#searchResult",\n  "etag": etag,\n  "id": {\n    "kind": string,\n    "videoId": string,\n    "channelId": string,\n    "playlistId": string\n  },\n  "snippet": {\n    "publishedAt": datetime,\n    "channelId": string,\n    "title": string,\n    "description": string,\n    "thumbnails": {\n      (key): {\n        "url": string,\n        "width": unsigned integer,\n        "height": unsigned integer\n      }\n    },\n    "channelTitle": string,\n    "liveBroadcastContent": string\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n