构建 YouTube 集成通常需要回答三个问题:这是什么视频、观众在说什么,以及他们接下来应该看什么?读完本指南后,你就可以调用 Youtube Data API 获取视频详情、最新评论和相关视频,并在 Python 应用中使用返回的字段。
Youtube Data API 返回的内容
Youtube Data API 提供三个用于处理视频 ID 的 GET 端点。Video Details 返回标题、说明、发布日期、观看次数、评论数和点赞数等信息。Video Comments 返回最新评论,包括评论文本、作者、创建日期和点赞数。Similar Videos 返回相关视频列表。
这些操作适用于视频发现界面、创作者仪表板和编辑研究工具等应用。例如,应用可以加载视频详情,在旁边显示相关标题,并呈现最新评论供研究人员查看。
开始使用 Zyla API Hub
打开 Zyla API Hub 上的 Youtube Data API 页面,如有需要,请创建免费账户,并在可用时选择 Subscribe 或 Start Free Trial。从账户中获取 API 密钥,并在 Authorization: Bearer YOUR_API_KEY 标头中使用该密钥。
Zyla 采用订阅和配额模式,而非按次付费。首个 API 提供 7 天试用或 50 次请求;请查看 API 页面,了解当前的访问选项和价格。该市场拥有 10,000 多个公共 API,通过一个账户和 API 密钥即可访问整个 Hub。
可用端点
这三种操作都使用 GET 请求和相同的 Bearer 身份验证标头。以下 URL 是 Hub 请求 URL;只需提供所调用操作文档中列出的参数。
Similar Videos
GET https://zylalabs.com/api/14/youtube-data-api/56/similar-videos
提供视频 ID,即可获取相似视频列表。请求参数如下:
relatedToVideoId— 必填字符串,包含源视频 ID,例如7ghhRHRP6t4。part— 必填字符串;文档示例使用id,snippet。type— 必填字符串;文档示例使用video。maxResults— 可选字符串;文档示例使用50。
返回的条目包含视频标识符和摘要信息。示例响应还包含 pageInfo 和 nextPageToken,有助于了解结果集。
Video Comments
GET https://zylalabs.com/api/14/youtube-data-api/61/video-comments
获取视频的最新评论列表。必填字符串参数为 part、videoId 和 maxResults;文档中对应的值分别为 snippet、7ghhRHRP6t4 和 100。
文档中的响应将评论嵌套在 snippet.topLevelComment.snippet 下。其中的字段包括 textDisplay、textOriginal、authorDisplayName、likeCount、publishedAt 和 updatedAt。评论线程还包含 totalReplyCount;应将其视为数量,而不是回复文本。
Video Details
GET https://zylalabs.com/api/14/youtube-data-api/62/video-details
传入必填字符串参数 id,即可获取视频详情。文档示例使用 7ghhRHRP6t4。响应包含一个 items 数组,其中有视频 id 和一个 snippet,后者包含 title、description、publishedAt 和 channelId 等字段。
此端点还会提供观看次数、评论数和点赞数等信息。集成这些值时,请参考 API 页面,而不要假设它们的字段路径与 Similar Videos 响应相同。
调用 Similar Videos 并读取结果
此请求使用文档中的视频 ID,并请求 id 和 snippet 部分。运行前,请将 YOUR_API_KEY 替换为你的 Hub 密钥。
curl -s -X GET "https://zylalabs.com/api/14/youtube-data-api/56/similar-videos?relatedToVideoId=7ghhRHRP6t4&part=id%2Csnippet&type=video&maxResults=50" \
-H "Authorization: Bearer YOUR_API_KEY"
官方响应摘录如下所示。内容在第一条目的缩略图数据中途截断,因此它展示了返回的字段,但并不是可以直接粘贴到解析器中的完整 JSON 文档。
{"kind":"youtube#searchListResponse","nextPageToken":"CDIQAA","pageInfo":{"totalResults":108,"resultsPerPage":50},"items":[{"kind":"youtube#searchResult","id":{"kind":"youtube#video","videoId":"mj0XInqZMHY"},"snippet":{"publishedAt":"2019-06-27T14:44:32Z","channelId":"UC0C-w0YjGpqDXGB8IHb662A","title":"Ed Sheeran - Beautiful People (feat. Khalid) [Official Music Video]","description":"The official music video for Ed Sheeran - Beautiful People (feat. @khalid)\nSubtract, the new album, out 05.05.2023. Pre-order: https://es.lnk.to/subtract\n \nTaken from No.6 Collaborations Project released in 2019, which featured the hit singles 'I Don't Care', 'Cross Me', 'Beautiful People', 'Blow', 'Best Part of Me', 'Antisocial', 'South of the Border' &'Take Me Back To London'. \n \nSubscribe to the Ed Sheeran channel for all the best and latest official music videos, behind the scenes and live performances. \nhttp://bit.ly/SubscribeToEdSheeran \n \nSee more official videos from Ed Sheeran here: \nhttps://www.youtube.com/watch?v=ryJgDL9jzKk&list=PLjp0AEEJ0-fGKG_3skl0e1FQlJfnx-TJz \n\nListen to more from the No. 6 Collaborations Project here : \nhttps://www.youtube.com/playlist?list=PLjp0AEEJ0-fHZDWhxhKo0wG08bMUcRy3l\n \nFollow Ed Sheeran on: \nInstagram: http://instagram.com/teddysphotos\nTikTok: https://www.tiktok.com/@edsheeran\nFacebook: http://www.facebook.com/EdSheeranMusic\nTwitter: http://twitter.com/edsheeran\nDiscord: http://discord.gg/edsheeran\nOfficial Website: http://edsheeran.com \n\nAbout Ed Sheeran: \nEd Sheeran may be the quintessential pop star of the 2010s: an internationally acclaimed, multi-award winning singer/songwriter who seems to acknowledge no boundaries between styles or eras of music. \n\nHis incredible catalogue includes the studio albums ‘+’ (plus), ‘x’ (multiply) and ‘÷’ (divide) which spawned hit singles such as ‘The A team’ , ’Lego House’, ‘Sing', ‘Thinking Out Loud’, ‘Photograph’, ‘Shape Of You’, ‘Castle on The Hill’ and ‘Perfect’. \n \nIn 2019, Ed Sheeran released the genre-spanning ‘No.6 Collaborations Project’ which featured a wide-range of artists including ‘Justin Bieber’, ‘Camilla’ Cabello’, ‘Travis Scott’, ‘Eminem’, ‘Cardi B, ‘Paulo Londra’, ‘Bruno Mars’ and ‘Stormzy’ amongst many others, producing hits such as ‘I Don’t Care’, ‘Beautiful People’, ‘South of The Border’ and ‘Take Me Back To London’. \n \n#EdSheeran #Khalid #BeautifulPeople","thumbnails":{"…
对于 items 中的每个条目,使用 id.videoId 作为相关视频的标识符,并使用 snippet.title 作为显示标题。摘要还包含 publishedAt 和 channelId。示例时间戳末尾的 Z 表示 UTC。
pageInfo.totalResults 和 pageInfo.resultsPerPage 描述结果集,示例中还出现了 nextPageToken。请将该令牌视为响应数据:所提供的端点参数并未说明如何请求后续页面。
Python 示例
以下示例使用相同的参数调用同一端点,并打印每个返回的视频 ID 和标题。它使用 Python 标准库,并从 API 读取完整响应,而不是上面的截断摘录。
import json
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
url = (
"https://zylalabs.com/api/14/youtube-data-api/56/similar-videos"
"?relatedToVideoId=7ghhRHRP6t4&part=id%2Csnippet"
"&type=video&maxResults=50"
)
request = Request(
url,
headers={"Authorization": "Bearer YOUR_API_KEY"},
method="GET",
)
try:
with urlopen(request, timeout=30) as response:
data = json.load(response)
except HTTPError as error:
raise SystemExit(f"API request failed: HTTP {error.code}")
except URLError as error:
raise SystemExit(f"Could not reach the API: {error.reason}")
for item in data.get("items", []):
video_id = item.get("id", {}).get("videoId")
title = item.get("snippet", {}).get("title")
if video_id and title:
print(f"{video_id}: {title}")
该示例可以处理缺少 items 数组的情况,并跳过没有 ID 或标题的条目。在应用中,应区分空列表和请求失败,避免界面将错误显示为“没有相关视频”。请将 API 密钥保存在服务器端配置中,不要在客户端应用中暴露。
实际应用场景
构建相关视频面板
用户打开视频页面时,使用该视频的 ID 调用 Similar Videos。呈现每个返回的 snippet.title,并保留 id.videoId,以便选择时能够识别下一个视频。返回的 channelId 和发布日期时间戳也可提供更多背景信息。
根据应用的需求缓存结果一段时间,而不是每次渲染页面时都请求相同的列表。相关结果可能会变化,因此应使缓存可以刷新,并避免将缓存列表视为永久不变。
支持创作者和编辑研究
使用 Video Details 将视频标题、说明、发布日期和互动信息集中展示。编辑团队可以利用这些背景信息决定要审查哪些视频;创作者仪表板则可以将相同信息与应用中已有的其他数据一并显示。
缓存结果时,请保存视频 ID,避免详情与源视频脱离关联。观看次数、评论数和点赞数都可能变化,应将其视为观测值,而不是视频不可变的属性。
查看近期观众反馈
针对正在审查的视频调用 Video Comments,并显示评论文本、作者、创建日期和点赞数。这些字段有助于团队浏览近期反馈,而不必将每条评论都简化为单一评分。
文档中的响应同时包含 textDisplay 和 textOriginal。请选择适合展示需求的字段,并在渲染时将返回的文本视为不可信内容。线程的回复数可作为有用的背景信息,但文档中的请求并未说明有单独的操作可用于获取回复。
通过 AI 智能体使用 API
Zyla API 也可以通过 MCP 端点调用。使用 Claude Code、Cursor 或 Windsurf 等兼容 MCP 的客户端进行配置,地址为 https://mcp.zylalabs.com/mcp?apikey=YOUR_API_KEY,并将占位符替换为你的 Hub 密钥。
智能体可以在工作流中使用可用的 API 操作,例如收集视频详情和最新评论以供审查。请查阅客户端和 Hub 工具文档,了解确切的工具接口,并避免将密钥放入提示词或共享日志中。
常见问题
如何查找与某个 YouTube 视频相似的视频?
使用源视频 ID 作为 relatedToVideoId,并提供必填的 part 和 type 参数,调用 Similar Videos。从 items[].id.videoId 读取每个相关视频的 ID,并从 items[].snippet.title 读取标题。
我可以获取视频的最新评论吗?
可以。使用 part、videoId 和 maxResults 调用 Video Comments。文档中的响应包含评论文本、作者、发布日期和点赞数。
我可以使用返回的令牌请求下一页吗?
示例响应包含 nextPageToken,但所提供的请求参数并未定义分页参数。实现后续页面请求前,请查看 API 页面。
访问费用如何计算?
访问采用订阅和配额模式,而非按次付费。首个 API 提供 7 天试用或 50 次请求;请查看页面,了解当前的访问选项和价格。
创建免费的 Zyla 账户并获取 API 密钥,然后从市场页面开始 Youtube Data API 的 7 天试用。
官方 API 响应
{"kind":"youtube#searchListResponse","nextPageToken":"CDIQAA","pageInfo":{"totalResults":108,"resultsPerPage":50},"items":[{"kind":"youtube#searchResult","id":{"kind":"youtube#video","videoId":"mj0XInqZMHY"},"snippet":{"publishedAt":"2019-06-27T14:44:32Z","channelId":"UC0C-w0YjGpqDXGB8IHb662A","title":"Ed Sheeran - Beautiful People (feat. Khalid) [Official Music Video]","description":"The official music video for Ed Sheeran - Beautiful People (feat. @khalid)\nSubtract, the new album, out 05.05.2023. Pre-order: https://es.lnk.to/subtract\n \nTaken from No.6 Collaborations Project released in 2019, which featured the hit singles 'I Don't Care', 'Cross Me', 'Beautiful People', 'Blow', 'Best Part of Me', 'Antisocial', 'South of the Border' &'Take Me Back To London'. \n \nSubscribe to the Ed Sheeran channel for all the best and latest official music videos, behind the scenes and live performances. \nhttp://bit.ly/SubscribeToEdSheeran \n \nSee more official videos from Ed Sheeran here: \nhttps://www.youtube.com/watch?v=ryJgDL9jzKk&list=PLjp0AEEJ0-fGKG_3skl0e1FQlJfnx-TJz \n\nListen to more from the No. 6 Collaborations Project here : \nhttps://www.youtube.com/playlist?list=PLjp0AEEJ0-fHZDWhxhKo0wG08bMUcRy3l\n \nFollow Ed Sheeran on: \nInstagram: http://instagram.com/teddysphotos\nTikTok: https://www.tiktok.com/@edsheeran\nFacebook: http://www.facebook.com/EdSheeranMusic\nTwitter: http://twitter.com/edsheeran\nDiscord: http://discord.gg/edsheeran\nOfficial Website: http://edsheeran.com \n\nAbout Ed Sheeran: \nEd Sheeran may be the quintessential pop star of the 2010s: an internationally acclaimed, multi-award winning singer/songwriter who seems to acknowledge no boundaries between styles or eras with elements of folk, hip-hop, pop, dance, soul, and rock woven throughout his music. \n\nHis incredible catalogue includes the studio albums ‘+’ (plus), ‘x’ (multiply) and ‘÷’ (divide) which spawned hit singles such as ‘The A team’ , ’Lego House’, ‘Sing', ‘Thinking Out Loud’, ‘Photograph’, ‘Shape Of You’, ‘Castle on The Hill’ and ‘Perfect’. \n \nIn 2019, Ed Sheeran released the genre-spanning ‘No.6 Collaborations Project’ which featured a wide-range of artists including ‘Justin Bieber’, ‘Camilla’ Cabello’, ‘Travis Scott’, ‘Eminem’, ‘Cardi B, ‘Paulo Londra’, ‘Bruno Mars’ and ‘Stormzy’ amongst many others, producing hits such as ‘I Don’t Care’, ‘Beautiful People’, ‘South of The Border’ and ‘Take Me Back To London’. \n \n#EdSheeran #Khalid #BeautifulPeople","thumbnails":{"…
官方 API 响应
{"kind":"youtube#searchListResponse","nextPageToken":"CDIQAA","pageInfo":{"totalResults":108,"resultsPerPage":50},"items":[{"kind":"youtube#searchResult","id":{"kind":"youtube#video","videoId":"mj0XInqZMHY"},"snippet":{"publishedAt":"2019-06-27T14:44:32Z","channelId":"UC0C-w0YjGpqDXGB8IHb662A","title":"Ed Sheeran - Beautiful People (feat. Khalid) [Official Music Video]","description":"The official music video for Ed Sheeran - Beautiful People (feat. @khalid)\nSubtract, the new album, out 05.05.2023. Pre-order: https://es.lnk.to/subtract\n \nTaken from No.6 Collaborations Project released in 2019, which featured the hit singles 'I Don't Care', 'Cross Me', 'Beautiful People', 'Blow', 'Best Part of Me', 'Antisocial', 'South of the Border' &'Take Me Back To London'. \n \nSubscribe to the Ed Sheeran channel for all the best and latest official music videos, behind the scenes and live performances. \nhttp://bit.ly/SubscribeToEdSheeran \n \nSee more official videos from Ed Sheeran here: \nhttps://www.youtube.com/watch?v=ryJgDL9jzKk&list=PLjp0AEEJ0-fGKG_3skl0e1FQlJfnx-TJz \n\nListen to more from the No. 6 Collaborations Project here : \nhttps://www.youtube.com/playlist?list=PLjp0AEEJ0-fHZDWhxhKo0wG08bMUcRy3l\n \nFollow Ed Sheeran on: \nInstagram: http://instagram.com/teddysphotos\nTikTok: https://www.tiktok.com/@edsheeran\nFacebook: http://www.facebook.com/EdSheeranMusic\nTwitter: http://twitter.com/edsheeran\nDiscord: http://discord.gg/edsheeran\nOfficial Website: http://edsheeran.com \n\nAbout Ed Sheeran: \nEd Sheeran may be the quintessential pop star of the 2010s: an internationally acclaimed, multi-award winning singer/songwriter who seems to acknowledge no boundaries between styles or eras with elements of folk, hip-hop, pop, dance, soul, and rock woven throughout his music. \n\nHis incredible catalogue includes the studio albums ‘+’ (plus), ‘x’ (multiply) and ‘÷’ (divide) which spawned hit singles such as ‘The A team’ , ’Lego House’, ‘Sing', ‘Thinking Out Loud’, ‘Photograph’, ‘Shape Of You’, ‘Castle on The Hill’ and ‘Perfect’. \n \nIn 2019, Ed Sheeran released the genre-spanning ‘No.6 Collaborations Project’ which featured a wide-range of artists including ‘Justin Bieber’, ‘Camilla’ Cabello’, ‘Travis Scott’, ‘Eminem’, ‘Cardi B, ‘Paulo Londra’, ‘Bruno Mars’ and ‘Stormzy’ amongst many others, producing hits such as ‘I Don’t Care’, ‘Beautiful People’, ‘South of The Border’ and ‘Take Me Back To London’. \n \n#EdSheeran #Khalid #BeautifulPeople","thumbnails":{"…