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

如何通过API、Ajax等方式获取YouTube音乐视频指定元数据?

Great question! I’ve dealt with similar metadata-grabbing challenges for school projects before, so let’s break down your best options to get that structured info from the ytd-metadata-row-container-renderer div you spotted:

This is the most reliable, TOS-compliant way to pull the metadata you need—no messy HTML parsing required. Here’s how to target your specific requirements:

  • Exact Song Title & Artist: For official music videos, the Videos.list endpoint (with part=snippet) returns the video’s title (usually formatted as [Song Title] - [Artist]) in snippet.title. You can also dig into snippet.tags for more precise, tagged artist/song names. If the video is linked to YouTube Music, some entries will have additional music-specific fields in contentDetails (look for musicVideoType and related attributes).
  • Categories: Use the topicDetails part of the API response—topicCategories will give you the video’s assigned categories, and snippet.categoryId maps to YouTube’s official category list (you can cross-reference this ID with YouTube’s public category lookup).
  • License Info: The status section includes a license field (e.g., creativeCommon or youtube) that outlines the video’s licensing terms.

To implement this:

  1. Grab an API key from the Google Cloud Console (it’s free for limited usage, perfect for a school project).
  2. Make a GET request to https://www.googleapis.com/youtube/v3/videos with these parameters:
    • id: The YouTube video ID (extracted from the video URL)
    • part: snippet,contentDetails,status,topicDetails
    • key: Your API key

The response will be a clean JSON object with all the structured data you need—no need to hunt through HTML elements.

2. Web Scraping (Use with Extreme Caution)

If the official API doesn’t cover every piece of metadata you see in that div, you could scrape the video page, but this violates YouTube’s Terms of Service and will break if YouTube updates their page structure (which they do often).

If you still want to explore this route:

  • You can’t use client-side Ajax directly due to CORS restrictions—you’ll need a server-side script (e.g., Node.js with axios + cheerio, or Python with requests + BeautifulSoup) to fetch the video page HTML.
  • Once you have the HTML, parse it to target the ytd-metadata-row-container-renderer elements. Each row has a label and value pair that you can extract into a usable object.

Example Node.js script with Cheerio:

const axios = require('axios');
const cheerio = require('cheerio');

async function scrapeYouTubeMetadata(videoId) {
  const videoUrl = `https://www.youtube.com/watch?v=${videoId}`;
  const { data } = await axios.get(videoUrl);
  const $ = cheerio.load(data);

  const metadata = {};
  $('ytd-metadata-row-container-renderer').each((_, element) => {
    const label = $(element).find('div:nth-child(1)').text().trim();
    const value = $(element).find('div:nth-child(2)').text().trim();
    metadata[label] = value;
  });

  return metadata;
}

3. Alternative: YouTube Music API

If your project is focused exclusively on music videos, the YouTube Music API (part of Google’s API ecosystem) might offer more granular, music-specific metadata—like exact song titles, album info, and artist discography details—that’s tailored to music content. It’s worth checking out if the standard YouTube Data API doesn’t give you everything you need.


Final tip: Stick with the official APIs whenever possible. They’re designed for this use case, stable, and won’t risk getting your project blocked by YouTube.

内容的提问来源于stack exchange,提问作者Simone Fabbri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:34:27