You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法用YouTube API v2获取播放列表文件ID,寻求解决方法

Getting Video IDs from a YouTube Playlist (Alternative to Deprecated API v2)

Got it, let's sort this out—YouTube API v2 has been long deprecated, so it's totally expected that it's not working for your use case. Here are three reliable, practical ways to pull video IDs from a playlist:

This is the most stable and compliant method, since it's Google's supported replacement for v2. Here's how to do it:

  • First, head to the Google Cloud Console, create a new project, enable the YouTube Data API v3, and generate an API key (make sure to restrict it to avoid misuse).
  • Use the PlaylistItems.list endpoint to fetch video details. You'll need to specify:
    • part=contentDetails (we only need the video ID metadata)
    • playlistId set to your target playlist's ID
    • Your API key via the key parameter
  • Example curl request:
curl "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=YOUR_PLAYLIST_ID&key=YOUR_API_KEY"
  • In the JSON response, each video's ID lives at items[].contentDetails.videoId. For long playlists, use the pageToken parameter to paginate through results.

2. Use yt-dlp (Command-Line Tool)

If you don't want to deal with API setup, yt-dlp is a fantastic open-source tool that handles this effortlessly:

  • Install yt-dlp (follow the official docs for your OS—no API key needed)
  • Run this command to extract all video IDs from a playlist:
yt-dlp --get-id https://www.youtube.com/playlist?list=YOUR_PLAYLIST_ID
  • The output will be each video ID on a new line, perfect for scripting or saving to a file. yt-dlp automatically handles pagination and bypasses most of YouTube's anti-scraping measures.

3. Web Scraping (Last Resort)

While not ideal (YouTube's terms of service restrict unauthorized scraping, and page structures change often), you can use Python to parse the playlist page if needed:

  • Example code with requests and BeautifulSoup:
import requests
from bs4 import BeautifulSoup

playlist_url = "https://www.youtube.com/playlist?list=YOUR_PLAYLIST_ID"
# Mimic a browser to avoid being blocked
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"}

response = requests.get(playlist_url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")

video_ids = set()  # Use a set to avoid duplicates
for anchor in soup.find_all("a", href=True):
    if "/watch?v=" in anchor["href"]:
        # Extract the video ID from the URL parameter
        vid_id = anchor["href"].split("v=")[1].split("&")[0]
        video_ids.add(vid_id)

print("\n".join(video_ids))
  • Note: This might break if YouTube updates its page layout, and you need to avoid making too many requests too quickly to avoid being blocked.

Final Notes

Stick with the YouTube Data API v3 for long-term projects—it's the only supported method. yt-dlp is great for quick one-off tasks. Web scraping should only be used if the other options aren't feasible, and always respect YouTube's terms.

内容的提问来源于stack exchange,提问作者Wojciech Urbaniak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:20:21