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

如何通过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:

1. Scrape the Video Page (Proceed With Caution)

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.

2. Leverage YouTube Data API v3 (Limited Coverage)

While there’s no full card endpoint, you can retrieve some specific card types via existing API endpoints:

  • Linked Playlists/Channels: Use the videos.list endpoint with part=contentDetails—the relatedPlaylists field might include playlists linked via cards (though this isn’t guaranteed for all cases).
  • Live Donation Cards: For live videos, videos.list with part=liveStreamingDetails may include a donationLink if a donation card is active.
  • Merchandise Cards: Channel-level merch cards can be fetched via channels.list with part=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.

3. Third-Party Tools/Libraries (Use At Your Own Risk)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:21:57