如何通过YouTube APIs获取指定视频ID关联的全部卡片?
Hey there, I’ve dealt with this exact problem before—frustratingly enough, YouTube’s official APIs don’t offer a dedicated endpoint to fetch all the cards linked to a specific video directly. But there are a few workarounds that can get you the data you need, depending on your use case:
YouTube embeds most card data (like annotation cards, link cards, donation prompts, etc.) directly in the video page’s source code. You can extract this using web scraping tools, but you must adhere to YouTube’s Terms of Service and robots.txt—avoid large-scale or automated scraping, as this can get your IP blocked.
A common approach is to pull the ytInitialPlayerResponse variable from the page source, which often contains structured JSON with card and annotation data. Here’s a quick Python example:
import requests import re import json video_id = "YOUR_TARGET_VIDEO_ID" url = f"https://www.youtube.com/watch?v={video_id}" # Mimic a browser user agent to avoid immediate blocks 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(url, headers=headers) # Extract the player response JSON match = re.search(r'var ytInitialPlayerResponse = ({.*?});', response.text, re.DOTALL) if match: player_data = json.loads(match.group(1)) # Access card/annotation data cards = player_data.get("cards", {}) annotations = player_data.get("annotations", {}) print("Found Cards:") print(json.dumps(cards, indent=2)) print("\nFound Annotations:") print(json.dumps(annotations, indent=2))
Note that this method is fragile—YouTube frequently updates its page structure, so the regex or JSON paths might break without warning. For age-restricted or login-gated videos, you’ll need a tool like Selenium to simulate a logged-in browser session.
While there’s no full card endpoint, you can retrieve some specific card types via existing API endpoints:
- Linked Playlists/Channels: Use the
videos.listendpoint withpart=contentDetails—therelatedPlaylistsfield might include playlists linked via cards (though this isn’t guaranteed for all cases). - Live Donation Cards: For live videos,
videos.listwithpart=liveStreamingDetailsmay include adonationLinkif a donation card is active. - Merchandise Cards: Channel-level merch cards can be fetched via
channels.listwithpart=brandingSettings, but this doesn’t tie merch to specific videos.
This approach only covers a subset of card types, but it’s the most reliable (and TOS-compliant) option for supported data.
Tools like yt-dlp (a fork of youtube-dl) parse video page data and expose card/annotation information in their output. For example, you can run:
yt-dlp --dump-json https://www.youtube.com/watch?v=YOUR_VIDEO_ID | jq '.annotations, .cards'
Like web scraping, these tools depend on YouTube’s page structure and can break at any time. They’re best for one-off checks rather than production systems.
内容的提问来源于stack exchange,提问作者elliott

