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

如何通过YouTube Data API V3获取视频分类?

获取YouTube视频分类信息的方法

你当前用的Search API只返回基础搜索结果,拿不到分类信息,得分两步走:

步骤1:获取视频ID后调用Videos API拿分类ID

先从搜索结果里提取视频ID,再调用youtube/v3/videos接口,就能拿到该视频的categoryId。

修改后的代码示例:

import requests

# 1. 搜索获取视频ID(限定只返回视频)
search_resp = requests.get('https://www.googleapis.com/youtube/v3/search', 
                    params= {
                        "key":"My Key",
                        "part":"snippet",
                        "q":"IT",
                        "maxResults":1,
                        "order":"rating",
                        "regionCode":"KR",
                        "type":"video"
                        })
video_id = search_resp.json()['items'][0]['id']['videoId']

# 2. 调用Videos API获取分类ID
video_resp = requests.get('https://www.googleapis.com/youtube/v3/videos',
                    params={
                        "key":"My Key",
                        "part":"snippet",
                        "id": video_id
                    })
category_id = video_resp.json()['items'][0]['snippet']['categoryId']
print(f"视频分类ID:{category_id}")

步骤2:调用VideoCategories API获取分类名称

如果需要拿到分类的具体名称(比如"科技""娱乐"),用刚才拿到的categoryId调用youtube/v3/videoCategories接口:

# 3. 调用VideoCategories API获取对应地区的分类名称
category_resp = requests.get('https://www.googleapis.com/youtube/v3/videoCategories',
                    params={
                        "key":"My Key",
                        "part":"snippet",
                        "id": category_id,
                        "regionCode":"KR"
                    })
category_name = category_resp.json()['items'][0]['snippet']['title']
print(f"视频分类名称:{category_name}")

注意事项

  • 必须加"type":"video"参数,避免Search API返回频道或播放列表(这类结果没有videoId)
  • 所有API调用都要带上有效的API Key,确保权限正常
  • 分类名称会随地区变化,要和搜索时的regionCode保持一致

内容的提问来源于stack exchange,提问作者오지훈

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 06:01:03