youtube API 从频道获取所有播放列表 ID:python

Posted

技术标签:

【中文标题】youtube API 从频道获取所有播放列表 ID:python【英文标题】:youtube API get all playlist id from a channel : python 【发布时间】:2020-10-02 10:38:17 【问题描述】:

我正在尝试在 youtube API 中列出来自特定频道的所有播放列表...在 python 中 我想在一个数组中有一个 playlist_id 列表

all_playlist_item = []

如果我启动 https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UC1udnO-W6gpR9qzleJ5SDKw&key=xxxxxx

这是此请求的响应

 
  "kind": "youtube#playlistListResponse",
  "etag": "PMwo-9BIp7p_L2ynH9sFOGIOido",
  "nextPageToken": "CAUQAA",
  "pageInfo": 
    "totalResults": 17,
    "resultsPerPage": 5
  ,
  "items": [
    
      "kind": "youtube#playlist",
      "etag": "-V67IpyB9a1JGDGJ4pVQnEoMRy4",
      "id": "PLWi7PxnyAMeN1tb-ldDzJcnJy2yd5wYrO",
      "snippet": 

...

我必须使用 nextPageToken,但我不知道如何为播放列表项编写此函数

这是我的代码(在 Excel 中仅从特定播放列表中提取所有视频)

channel_name="UC1udnO-W6gpR9qzleJ5SDKw"
playlist_name="PLWi7PxnyAMeOJmVLv8Z_N3edNyipsnHbo"



api_key = "xxxxxx"

from apiclient.discovery import build
import pandas as pd

youtube = build('youtube', 'v3', developerKey=api_key)



def get_channel_videos(channel_id):

# get Uploads playlist id
res = youtube.channels().list(id=channel_id, 
                              part='contentDetails').execute()
playlist_id = res['items'][0]['contentDetails']['relatedPlaylists']['uploads']

videos = []
next_page_token = None

while 1:
    res = youtube.playlistItems().list(playlistId=playlist_name, 
                                       part='snippet', 
                                       maxResults=50,
                                       pageToken=next_page_token).execute()
    videos += res['items']
    next_page_token = res.get('nextPageToken')

    if next_page_token is None:
        break

return videos

videos = get_channel_videos(channel_name)


def get_videos_stats(video_ids):
stats = []
for i in range(0, len(video_ids), 50):
    res = youtube.videos().list(id=','.join(video_ids[i:i+50]),
                               part='statistics').execute()
    stats += res['items']

return stats

video_ids = list(map(lambda x:x['snippet']['resourceId']['videoId'], videos))

stats = get_videos_stats(video_ids)


d = []
if len(stats)!=len(videos):
    i=1
    j=0
else:
    i=0
    j=0
len_video = len(videos)
len_stats = len(stats)
for video in videos:
    if i >= len_video:
        break
    Url_video='https://www.youtube.com/watch?v='+videos[i]['snippet']['resourceId']['videoId']+'&list='+playlist_name
    d.append((videos[i]['snippet']['title'], 
              videos[i]['snippet']['resourceId']['videoId'], 
              Url_video, 
              stats[j]['statistics']['viewCount'],
              stats[j]['statistics']['likeCount'],
              stats[j]['statistics']['dislikeCount']
              ))

    i+=1
    j+=1


df=pd.DataFrame(d, columns=('Titre_video', 'ID_video', 'Url_video','vues','like','dislike'))
df['vues'] = df['vues'].astype(int)
df['like'] = df['like'].astype(int)
df['dislike'] = df['dislike'].astype(int)
df.index+=1

df.to_excel("youtube-playlist.xlsx")

【问题讨论】:

【参考方案1】:

来自pagination doc,您可以使用:

youtube.playlistItems().list_next(request, response) 用于迭代播放列表项响应 youtube.playlists().list_next(request, response) 用于迭代通道响应

从播放列表中获取所有视频

import googleapiclient.discovery

playlist_id = "PLWi7PxnyAMeOJmVLv8Z_N3edNyipsnHbo"

youtube = googleapiclient.discovery.build("youtube", "v3", developerKey = "YOUR_API_KEY")

request = youtube.playlistItems().list(
    part = "snippet",
    playlistId = playlist_id,
    maxResults = 50
)
response = request.execute()

playlist_items = []
while request is not None:
    response = request.execute()
    playlist_items += response["items"]
    request = youtube.playlistItems().list_next(request, response)

print(f"total: len(playlist_items)")
print(playlist_items)

从频道获取所有播放列表

import googleapiclient.discovery

channel_id = "UC1udnO-W6gpR9qzleJ5SDKw"

youtube = googleapiclient.discovery.build("youtube", "v3", developerKey = "YOUR_API_KEY")

request = youtube.playlists().list(
    part = "snippet",
    channelId = channel_id,
    maxResults = 50
)
response = request.execute()

playlists = []
while request is not None:
    response = request.execute()
    playlists += response["items"]
    request = youtube.playlists().list_next(request, response)

print(f"total: len(playlists)")
print(playlists)

【讨论】:

以上是关于youtube API 从频道获取所有播放列表 ID:python的主要内容,如果未能解决你的问题,请参考以下文章

YouTube API用于获取频道上的所有视频

如何使用 YouTube API 获取频道名称?

Youtube api 从精选频道模块中获取频道

用于获取频道上所有视频的 Youtube Api(不再可用错误)

如何使用新的 YouTube Data API (V3) 获取某个频道的上传视频列表?

如何使用新的 YouTube Data API (V3) 获取某个频道的上传视频列表?