要获取带有视频标题的YouTube播放列表视频ID,您可以使用YouTube API
import os
from googleapiclient.discovery import build
# 请替换这里的YOUR_API_KEY为您的YouTube Data API密钥
api_key = "YOUR_API_KEY"
youtube = build("youtube", "v3", developerKey=api_key)
def get_playlist_videos(playlist_id):
videos = []
next_page_token = None
while True:
response = youtube.playlistItems().list(
playlistId=playlist_id,
part="snippet",
maxResults=50,
pageToken=next_page_token
).execute()
for item in response["items"]:
video_id = item["snippet"]["resourceId"]["videoId"]
video_title = item["snippet"]["title"]
videos.append({"id": video_id, "title": video_id})
next_page_token = response.get("nextPageToken")
if not next_page_token:
break
return videos
# 请替换这里的PLAYLIST_ID为您要查询的播放列表ID
playlist_id = "PLAYLIST_ID"
videos = get_playlist_videos(playlist_id)
for video in videos:
print(f"视频ID: {video['id']}, 视频标题: {video['title']}")
将其中的YOUR_API_KEY
替换为您的YouTube Data API密钥,将PLAYLIST_ID
替换为您要查询的播放列表ID。
运行此代码后,您将看到输出的视频ID和视频标题列表。
领取专属 10元无门槛券
手把手带您无忧上云