如何使用Youtube Data API v3在YouTube频道中获取所有视频节目?

问题描述 投票:0回答:1

我正在使用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.x youtube youtube-api google-api-python-client youtube-data-api
1个回答
1
投票

首先,您要打印特定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 demo中测试这些结果:

{
 "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's uploaded videos,你会得到他确实有19个上传的视频(数量与videoCount值相同)。


在你的问题中你说:

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

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

他的所有35个视频都在他的频道中显示在他的"videos" tab中。

总而言之,这19个视频对应于他上传的19个视频(分组在他的“上传”播放列表中)。如果您想要检索他的所有视频,您拥有的一个选项就是检索YouTube频道所拥有的所有播放列表。

对于这种情况,那些视频不是真正的频道,而是在一个单独的自动生成的YouTube频道,因此,混乱。

© www.soinside.com 2019 - 2024. All rights reserved.