首页 > 解决方案 > 如何使用 Youtube Data API v3 获取 youtube 频道中的所有视频标题?

问题描述

我正在使用 Youtube Data API v3 提取 youtube 频道中所有视频的标题。

我关注了来自https://developers.google.com/youtube/v3/code_samples/python的片段

我在查询时收到一个号码['statistics']['videoCount']

但是当我在 youtube 中搜索实际频道时,它会给出不同的视频数量。

假设我正在尝试 ID 为 - UCeLHszkByNZtPKcaVXOCOQQ的频道

['statistics']['videoCount']19 _

但是,如果我在 youtube 上搜索Post Malone频道,它里面有36 个视频。我哪里错了?

实际上是否['statistics']['videoCount']在 youtube 频道中提供了确切数量的视频?

这是我的代码:

from pprint import pprint
from googleapiclient.discovery import build
import os

YOUTUBE_API_KEY = os.environ.get('YOUTUBE_API_KEY')
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

lis = ['UCeLHszkByNZtPKcaVXOCOQQ']
for i in lis:
    channels_response = youtube.channels().list(part='statistics', id=i).execute()
    print(i, channels_response['items'][0]['statistics']['videoCount'])
for i in lis:
    channels_response = youtube.channels().list(part='contentDetails', id=i).execute()
    for channel in channels_response['items']:
        uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
        playlistitems_list_request = youtube.playlistItems().list(
            playlistId=uploads_list_id,
            part="snippet",
            maxResults=50
          )
        while playlistitems_list_request:
            playlistitems_list_response = playlistitems_list_request.execute()
            for playlist_item in playlistitems_list_response["items"]:
                # pprint(playlist_item)
                title = playlist_item["snippet"]["title"]
                video_id = playlist_item["snippet"]["resourceId"]["videoId"]
                print(title, video_id)
            playlistitems_list_request = youtube.playlistItems().list_next(
                playlistitems_list_request, playlistitems_list_response
            )

标签: python-3.xyoutubeyoutube-apigoogle-api-python-clientyoutube-data-api

解决方案


首先,您要打印来自给定 YouTube 频道的视频数量(通过使用它的channel_id

获得 后channel_id,使用此请求检索以下数据:

  • 上传视频的数量(即它的videoCount
  • playlistid已上传视频的播放列表的。

这是请求:

https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UCeLHszkByNZtPKcaVXOCOQQ&fields=items(contentDetails%2Cid%2Csnippet(country%2Cdescription%2Ctitle)%2Cstatistics%2Cstatus)%2CnextPageToken%2CpageInfo%2CprevPageToken%2CtokenPagination&key={YOUR_API_KEY}

这些是 YouTube 频道的结果:Post Malone

您可以在Google API Explorer 演示中测试这些结果:

{
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "id": "UCeLHszkByNZtPKcaVXOCOQQ",
   "snippet": {
    "title": "Post Malone",
    "description": "The official Post Malone YouTube Channel.\nwww.postmalone.com"
   },
   "contentDetails": {
    "relatedPlaylists": {
     "uploads": "UUeLHszkByNZtPKcaVXOCOQQ",
     "watchHistory": "HL",
     "watchLater": "WL"
    }
   },
   "statistics": {
    "viewCount": "967939106",
    "commentCount": "0",
    "subscriberCount": "11072809",
    "hiddenSubscriberCount": false,
    "videoCount": "19"
   }
  }
 ]
}

检查这两个值:uploadsvideoCount

如果你进入Post Malone 的上传视频,你会发现他确实有 19 个上传的视频(与数值显示的数量相同videoCount


在你的问题中你说:

但是,如果我在 youtube 上搜索 Post Malone 频道,它里面有 36 个视频。我哪里错了?

我不认为你做错了什么,只是你没有完整的频谱。你看,如果你检查其中的一些playlists,你会看到 35 个视频对应于这些播放列表:

他所有的 35 个视频都显示在他频道的“视频”选项卡中。

综上所述,这 19 个视频对应于他上传的 19 个视频(在他的“上传”播放列表中分组)。如果您想检索他的所有视频,您可以选择检索 YouTube 频道拥有的所有播放列表。

在这种情况下,这些视频实际上并不在频道中,而是在单独的自动生成的 YouTube 频道中,因此造成了混乱。


推荐阅读