如何使用 Youtube Data API v3 获取 youtube 频道中的所有视频标题?

问题描述:

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

I'm working on extracting titles of all videos in a youtube channel using Youtube Data API v3.

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

我在查询 ['statistics']['videoCount']

但是当我在 youtube 上搜索实际频道时,它给出了不同的视频数量.

But when I search for the actual channel in youtube, it is giving a different number for count of videos.

假设我正在尝试 ID 为 - UCeLHszkByNZtPKcaVXOCOQQ

Let's say I'm trying for channel whose ID is - UCeLHszkByNZtPKcaVXOCOQQ

['statistics']['videoCount']19

但是,如果我在 youtube 上搜索频道发布马龙,它有36 个视频.我哪里出错了?

However if I search for the channel Post Malone on youtube, it has 36 videos in it. Where am I going wrong?

['statistics']['videoCount'] 是否确实给出了 youtube 频道中视频的确切数量?

Does the ['statistics']['videoCount'] actually give exact number of videos in a youtube channel?

这是我的代码:

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
            )

首先,您要打印给定 YouTube 频道中的视频数量(通过使用其 channel_id).

First, you're printing the number of videos from a given YouTube Channel (by using its channel_id).

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

Once you have the channel_id, use this request for retrieve the following data:

  • 上传视频的数量(即其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 频道的结果:发布马龙

These are the results of the YouTube channel: 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.

Check these two values: uploads and videoCount.

如果您进入发布马龙上传的视频,您会得到他确实上传了 19 个视频(与 videoCount 值中显示的数量相同).

If you enter to the Post Malone's uploaded videos, you'll get that he has indeed, 19 uploaded videos (the same quantity as shown in the videoCount value).

在你的问题中你说:

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

However if I search for the channel Post Malone on youtube, it has 36 videos in it. Where am I going wrong?

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

I don't think you're doing anything wrong, just you don't have the complete spectrum. You see, if you check some of its playlists, you'll see that the 35 videos corresponds to these playlists:

  • Music = 33 videos.
  • More about the artist = 2 videos.

他的所有 35 个视频都显示在他的 视频"标签在他的频道中.

All his 35 videos are shown in his "videos" tab in his channel.

总而言之,这 19 个视频对应于他上传的 19 个视频(在他的上传"播放列表中分组).如果您想检索他的所有视频,您可以选择的一种方法是检索 YouTube 频道拥有的所有播放列表.

To sum it up, the 19 videos corresponds to his 19 uploaded videos (that are grouped in his "uploads" playlist). If you want retrieve all his videos, one option you have is retrieve all the playlists that the YouTube channel has.

在这种情况下,这些视频实际上并不在 is 频道中,而是在一个单独的自动生成的 YouTube 频道中,因此会造成混淆.

For this case, those videos aren't in is channel really, but in a separate autogenerated YouTube channel, hence, the confusion.