【问题标题】:How to get all videos titles in a youtube channgel using Youtube Data API v3?如何使用 Youtube Data API v3 获取 youtube 频道中的所有视频标题?
【发布时间】:2019-07-27 09:16:59
【问题描述】:

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

我从https://developers.google.com/youtube/v3/code_samples/python关注了sn-ps

我在查询['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
            )

【问题讨论】:

  • 您的问题标题为:How to get all **videos titles** in a youtube channel,但在您的问题中,您要求视频计数...我不明白您的确切问题。您正在使用的请求的 URL 是哪个? - 请注意,某些视频可能不可用(可能 YouTube 频道将此类视频设置为不可嵌入),您的 youtube 频道示例确实有 35 个视频,但是,see its 19 videos available "i.e public" = 与返回的相同通过 API。
  • 我先打印视频的数量,然后再打印所有这些视频的标题。当您说不可嵌入时,我没有得到您。能否请您详细说明。
  • Sukumar,某些视频无法嵌入(由于其所有者禁用了此类选项),这可能是此类视频不计入的原因。其他选项是这些视频是私有的,YouTube API 将仅显示公共信息。我将根据您的评论发布答案,但是,答案将显示请求及其结果,而不是 python 代码(因为我不了解 Python)

标签: python-3.x youtube youtube-api google-api-python-client youtube-data-api


【解决方案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 频道中,因此会造成混乱。

【讨论】:

    【解决方案2】:

    此代码 sn-p 可让您从 youtube 播放列表中提取所有视频标题:

    import re 
    import requests
    
    url = "https://www.your_playlist_url.com"
    
    r = requests.get(url)
    
    code = r.text
    
    titles = re.findall("simpleText":"[^"]*"},"index", code)
    
    for i in titles: print(i[13: -9])`
    

    它只获取 HTML 并过滤掉视频标题,这些标题存储在一个复杂的 javascript 对象中。

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 2021-03-13
      • 2017-11-21
      • 2021-06-03
      • 2016-03-12
      • 2015-03-11
      • 2015-04-17
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多