关于 Google Trends API
Google Trends API 是用于获取 Google Trends 搜索兴趣数据的 Python 客户端,提供标准化的 0–100 指数、历史序列和增长指标。它面向需要在 Python 或 JavaScript/TypeScript 应用中分析搜索趋势的开发者。
主要功能
- 01获取关键词时间序列
- 02计算多个周期的增长指标
- 03读取实时趋势排行
- 04提供 Python 与 JavaScript 客户端
适用场景
- 01分析关键词搜索兴趣的历史变化
- 02比较搜索兴趣的周期增长
- 03将趋势数据整理为 Pandas 数据集
配置
远程服务无需本地安装。选择你使用的客户端,复制配置或使用一键安装。
https://google-trends.api.trendsapi.ai/mcpclaude mcp add --transport http ai-trendsapi-google-trends https://google-trends.api.trendsapi.ai/mcp{"mcpServers":{"ai-trendsapi-google-trends":{"type":"http","url":"https://google-trends.api.trendsapi.ai/mcp"}}}在 Cursor 中打开{"servers":{"ai-trendsapi-google-trends":{"type":"http","url":"https://google-trends.api.trendsapi.ai/mcp"}}}在 VS Code 中打开{"mcpServers":{"ai-trendsapi-google-trends":{"type":"http","url":"https://google-trends.api.trendsapi.ai/mcp"}}}{"mcpServers":{"ai-trendsapi-google-trends":{"serverUrl":"https://google-trends.api.trendsapi.ai/mcp"}}}命令在终端中运行;JSON 粘贴到对应客户端的 MCP 配置后重启。需要授权的服务会在首次连接时提示登录。
https://google-trends.api.trendsapi.ai/mcp需要授权工具
详细介绍
Google Trends API
通过 Trends API 获取 Google Trends 数据的 Python 客户端。提供标准化的 0-100 分数、历史数据和增长数据,并有免费套餐。不是爬虫。
密钥:trendsapi.ai/#get-key。HTTP 接口约定及所有源:trendsapi-ai/trendsapi。
身份验证
pip install google-trends-client
export TRENDSAPI_KEY=your_key
Python 3.9+。与 HTTP API 使用相同的密钥。
from google_trends_client import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
关键词辅助方法默认使用 source: "google search"。传入 source=,即可通过同一客户端访问其他平台。官方完整客户端(支持所有源,不设预设):trendsapi。
方法
| 方法 | REST mode | 返回值 |
|---|---|---|
get_time_series(keyword, source=, data_mode=) | get_time_series | list[TrendsDataPoint] |
get_growth(keyword, percent_growth=, source=, data_mode=) | get_growth | GetGrowthResponse |
get_live(limit=, offset=, category=) | get_top_trends | GetTopTrendsResponse |
get_top_trends(type=, ...) | get_top_trends | GetTopTrendsResponse |
source 使用小写(google search)。type 必须完全匹配(Google Trends)。混用会返回 400 错误。
from google_trends_client import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
series = client.get_time_series("heat pump")
print(series[-1].date, series[-1].value)
growth = client.get_growth("heat pump", 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("heat pump")
每个数据点包含:
| 字段 | 始终包含 | 含义 |
|---|---|---|
date | 是 | YYYY-MM-DD |
value | 是 | 该序列的 0-100 指数 |
keyword | 是 | 原样返回 |
volume | 否 | 可用时提供绝对搜索量 |
source 或 datatype | 否 | 数据管道标签 |
Python 返回 list[TrendsDataPoint]。使用 .date 和 .value,而不是 ["date"]。
JS 将相同字段作为对象属性返回。
get_growth
g = client.get_growth("heat pump", 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)。
异步
import asyncio
from google_trends_client import AsyncTrendsAPI
async def main():
c = AsyncTrendsAPI()
return await asyncio.gather(
c.get_time_series("heat pump"),
)
asyncio.run(main())
每个 200 响应计为一次计费请求。
Pandas
from dataclasses import asdict
import pandas as pd
from google_trends_client import TrendsAPI
df = pd.DataFrame(asdict(p) for p in TrendsAPI().get_time_series("heat pump"))
df["date"] = pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())
JavaScript / TypeScript
npm install google-trends-js
支持 Node 18+、Deno、Bun、Workers。使用相同的 API 密钥。上方的字段表同样适用。
另以 google-trends-node、google-trends-client 发布。同一客户端,不同的包名。
方法
| 方法 | 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 "google-trends-js";
const client = new TrendsAPI({ apiKey: process.env.TRENDSAPI_KEY! });
const series = await client.getTimeSeries("heat pump");
console.log(series.at(-1)?.date, series.at(-1)?.value);
const growth = await client.getGrowth("heat pump", {
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: google search |
| 关键词 | 任意短语,例如 heat pump |
实时 type | Google Trends |
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":"google search","keyword":"heat pump"}'
来源说明
- 相关来源(相同端点,不同的
source):google images、google news、google shopping。 value表示该词在 Google 网页搜索中的 0-100 分值,并非 Ads 展示次数。
错误
| HTTP | 客户端 |
|---|---|
| 200 | 已解析的负载。Python 数据类 / JS 类型化对象 |
| 400 | 抛出错误。修正 source 或 type 的拼写 |
| 401 | 抛出错误。检查 TRENDSAPI_KEY |
| 404 | 抛出错误。该关键词没有时间序列。不要重试 |
| 429 | 抛出错误。配额已用尽 |
| 5xx | 客户端重试,然后抛出错误 |
HTTP body 字段是 JSON 字符串。SDK 会对其解码。原始 curl 必须再次解析 body。
网站:https://trendsapi.ai/trends/google-trends。
许可证
MIT。参见 LICENSE。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。