mcpField
Y
YouTube Trends API

YouTube Trends API

远程服务
trendsapi.ai数据与分析12 周前更新

关于 YouTube Trends API

YouTube Trends API 提供客户端库和 HTTP API,用于获取 YouTube 搜索兴趣历史、增长情况及实时趋势。它适用于分析搜索兴趣随时间的变化或跟踪当前趋势的开发者。

主要功能

  1. 01获取关键词兴趣时间序列
  2. 02衡量所选周期的增长
  3. 03获取 YouTube 实时趋势
  4. 04使用 Python 和 JavaScript SDK
  5. 05调用 HTTP API

适用场景

  1. 01跟踪关键词兴趣随时间的变化
  2. 02比较关键词在不同周期的增长
  3. 03查看当前 YouTube 热门趋势

配置

远程服务无需本地安装。选择你使用的客户端,复制配置或使用一键安装。

服务地址https://youtube.api.trendsapi.ai/mcp
一键复制安装命令
Claude CodeTerminal
Cursor.cursor/mcp.json
VS Code.vscode/mcp.json
Claude Desktopclaude_desktop_config.json
Windsurf~/.codeium/windsurf/mcp_config.json

命令在终端中运行;JSON 粘贴到对应客户端的 MCP 配置后重启。需要授权的服务会在首次连接时提示登录。

全部服务地址
Streamable HTTPhttps://youtube.api.trendsapi.ai/mcp需要授权

工具

详细介绍

YouTube 搜索兴趣 API

通过 YouTube Trends API 获取 YouTube 搜索兴趣趋势。历史趋势、增长和 YouTube 实时热门内容。

License: MIT PyPI Python npm

密钥:trendsapi.ai/#get-key。HTTP 接口规范和所有数据源:trendsapi-ai/trendsapi。

身份验证

pip install trendsapi-youtube
export TRENDSAPI_KEY=your_key

Python 3.9+。与 HTTP API 使用相同的密钥。

from trendsapi_youtube import TrendsAPI

client = TrendsAPI()                    # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")

关键词辅助方法默认使用 source: "youtube"。传入 source= 即可通过同一个客户端访问其他平台。官方完整客户端(支持所有数据源,不预设数据源):trendsapi。

方法

方法REST mode返回值
get_time_series(keyword, source=, data_mode=)get_time_serieslist[TrendsDataPoint]
get_growth(keyword, percent_growth=, source=, data_mode=)get_growthGetGrowthResponse
get_live(limit=, offset=, category=)get_top_trendsGetTopTrendsResponse
get_top_trends(type=, ...)get_top_trendsGetTopTrendsResponse

source 使用小写(youtube)。type 必须完全匹配(YouTube Trending)。混用两者会返回 400 错误。

from trendsapi_youtube import TrendsAPI

client = TrendsAPI()                    # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")

series = client.get_time_series("sourdough starter")
print(series[-1].date, series[-1].value)

growth = client.get_growth("sourdough starter", percent_growth=["3M", "12M"])
print(growth.results[0].growth, growth.results[0].direction)

hot = client.get_live(limit=10)
print(hot.data)                         # [[1, "..."], ...]

get_time_series

points = client.get_time_series("sourdough starter")

每个数据点:

字段始终包含含义
date是YYYY-MM-DD
value是此序列的 0-100 指数
keyword是原样返回
volume否可用时提供绝对搜索量
source 或 datatype否数据管道标签

Python 返回 list[TrendsDataPoint]。使用 .date 和 .value,而不是 ["date"]。 JS 返回相同字段,作为对象属性提供。

get_growth

g = client.get_growth("sourdough starter", percent_growth=["12M", "3M", "YTD"])
print(g.results[0].growth, g.results[0].direction)

percent_growth 默认值:["12M"]。预设值:7D 14D 30D 1M 2M 3M 6M 9M 12M/1Y 18M 24M/2Y 36M/3Y 48M 60M/5Y MTD QTD YTD。自定义值:{"name": "Launch", "recent": "2024-06-01", "baseline": "2024-01-01"}。

字段含义
search_term关键词
data_source数据源
results每个时间窗口对应一个对象(period、growth、direction、日期、数值)
metadata计数 / 成功标志

多个时间窗口仍计为一次请求。Python:growth.results[0].growth。JS:growth.results[0].growth。

get_live

hot = client.get_live(limit=10)
字段含义
as_of_ts快照时间
type信息流名称
limit、offset、count分页
data[rank, label] 行

Python:hot.data。JS:hot.data。可选参数 offset= 和 category=(仅适用于 Amazon Best Sellers by Category、Top Websites)。

Async

import asyncio
from trendsapi_youtube import AsyncTrendsAPI

async def main():
    c = AsyncTrendsAPI()
    return await asyncio.gather(
        c.get_time_series("sourdough starter"),
        c.get_time_series("sourdough starter", source="google search"),
    )

asyncio.run(main())

每个 200 响应计为一次计费请求。

Pandas

from dataclasses import asdict
import pandas as pd
from trendsapi_youtube import TrendsAPI

df = pd.DataFrame(asdict(p) for p in TrendsAPI().get_time_series("sourdough starter"))
df["date"] = pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())

JavaScript / TypeScript

npm install trendsapi-youtube

支持 Node 18+、Deno、Bun、Workers。使用相同的 API 密钥。以上字段表同样适用。

方法
方法REST mode返回值
getTimeSeries(keyword, { source, data_mode })get_time_series每周数据点
getGrowth(keyword, { percent_growth, source, data_mode })get_growth增长对象
getLive({ limit, offset, category })get_top_trends实时信息流
getTopTrends({ type, ... })get_top_trends实时信息流
import { TrendsAPI } from "trendsapi-youtube";

const client = new TrendsAPI({ apiKey: process.env.TRENDSAPI_KEY! });
const series = await client.getTimeSeries("sourdough starter");
console.log(series.at(-1)?.date, series.at(-1)?.value);

const growth = await client.getGrowth("sourdough starter", {
  percent_growth: ["3M", "12M"],
});
console.log(growth.results[0].growth, growth.results[0].direction);

const live = await client.getLive({ limit: 10 });
console.log(live.data);                 // [[1, "..."], ...]

调用(curl)

字段值
端点POST https://api.trendsapi.ai/api
身份验证Authorization: Bearer $TRENDSAPI_KEY
历史数据使用 get_time_series 或 get_growth,并设置 source: youtube
关键词搜索词,例如 sourdough starter
实时 typeYouTube Trending
curl -sS -X POST https://api.trendsapi.ai/api \
  -H "Authorization: Bearer $TRENDSAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"mode":"get_time_series","source":"youtube","keyword":"sourdough starter"}'

数据源说明

  • value 是该词在 YouTube 上的 0-100 指数,不是视频元数据。
  • type 为 YouTube Trending,不是 youtube。
  • 此端点不支持 regionCode。

错误

HTTP客户端
200已解析的负载。Python 数据类 / JS 类型化对象
400抛出错误。修正 source 或 type 的拼写
401抛出错误。检查 TRENDSAPI_KEY
404抛出错误。该关键词没有对应的系列。请勿重试
429抛出错误。配额已用尽
5xx客户端重试后抛出错误

HTTP body 字段是一个 JSON 字符串。SDK 会对其解码。原始 curl 必须再解析一次 body。

网站:https://trendsapi.ai/trends/youtube-trends。

许可证

MIT。请参阅 LICENSE。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。