关于 News Sentiment API
News Sentiment API 提供新闻语气的时间序列与增长数据,适合追踪媒体情绪变化的开发者和分析人员,可通过 Python 客户端或 HTTP 调用。
主要功能
- 01新闻情绪时间序列
- 02预设或自定义周期增长
- 03Python 客户端与异步客户端
- 04支持 Pandas 数据处理
适用场景
- 01追踪公司相关的新闻语气
- 02比较不同时间段的情绪变化
配置
远程服务无需本地安装。选择你使用的客户端,复制配置或使用一键安装。
https://news-sentiment.api.trendsapi.ai/mcpclaude mcp add --transport http ai-trendsapi-news-sentiment https://news-sentiment.api.trendsapi.ai/mcp{"mcpServers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}在 Cursor 中打开{"servers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}在 VS Code 中打开{"mcpServers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}{"mcpServers":{"ai-trendsapi-news-sentiment":{"serverUrl":"https://news-sentiment.api.trendsapi.ai/mcp"}}}命令在终端中运行;JSON 粘贴到对应客户端的 MCP 配置后重启。需要授权的服务会在首次连接时提示登录。
https://news-sentiment.api.trendsapi.ai/mcp需要授权工具
详细介绍
新闻情绪 API
通过 Trends API 查看新闻情绪随时间的变化。无需抓取媒体网站,即可查看情绪历史和增长情况。
密钥:trendsapi.ai/#get-key。HTTP 契约和所有数据源:trendsapi-ai/trendsapi。
身份验证
pip install trendsapi-news-sentiment
export TRENDSAPI_KEY=your_key
Python 3.9+。与 HTTP API 使用相同的密钥。
from trendsapi_news_sentiment import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
关键词辅助方法默认使用 source: "news sentiment"。传入 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_top_trends(type=, ...) | get_top_trends | GetTopTrendsResponse |
source 使用小写(news sentiment)。type 必须完全匹配(Google Trends)。混用会返回 400 错误。
from trendsapi_news_sentiment import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
series = client.get_time_series("nvidia")
print(series[-1].date, series[-1].value)
growth = client.get_growth("nvidia", percent_growth=["3M", "12M"])
print(growth.results[0].growth, growth.results[0].direction)
get_time_series
points = client.get_time_series("nvidia")
每个数据点:
| 字段 | 是否必有 | 含义 |
|---|---|---|
date | 是 | YYYY-MM-DD |
value | 是 | 此时间序列的 0-100 指数 |
keyword | 是 | 回显的关键词 |
volume | 否 | 有数据时提供绝对量 |
source 或 datatype | 否 | 数据处理管道标签 |
Python 返回 list[TrendsDataPoint]。使用 .date 和 .value,而不是 ["date"]。
JS 返回相同字段,作为对象属性提供。
get_growth
g = client.get_growth("nvidia", 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。
异步
import asyncio
from trendsapi_news_sentiment import AsyncTrendsAPI
async def main():
c = AsyncTrendsAPI()
return await asyncio.gather(
c.get_time_series("nvidia"),
c.get_time_series("nvidia", source="google search"),
)
asyncio.run(main())
每个 200 响应计为一次计费请求。
Pandas
from dataclasses import asdict
import pandas as pd
from trendsapi_news_sentiment import TrendsAPI
df = pd.DataFrame(asdict(p) for p in TrendsAPI().get_time_series("nvidia"))
df["date"] = pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())
调用(curl)
| 字段 | 值 |
|---|---|
| 端点 | POST https://api.trendsapi.ai/api |
| 身份验证 | Authorization: Bearer $TRENDSAPI_KEY |
| 历史数据 | 使用 get_time_series 或 get_growth,并设置 source: news sentiment |
| 关键词 | 任意词组,例如 nvidia |
实时 type | 不适用 |
curl -sS -X POST https://api.trendsapi.ai/api \
-H "Authorization: Bearer $TRENDSAPI_KEY" \
-H "Content-Type: application/json" \
--max-time 60 \
-d '{"mode":"get_time_series","source":"news sentiment","keyword":"nvidia"}'
数据源说明
- 负的
growth表示情绪较上一时间窗口有所下降,并不代表该公司是负面的。 - 提及次数使用
news-trends-api(source: news volume)。 - 不提供单篇文章标签,也不提供媒体来源细分。
错误
| HTTP | 客户端 |
|---|---|
| 200 | 已解析的有效负载。Python 数据类 / JS 类型化对象 |
| 400 | 抛出错误。请修正 source 或 type 的拼写 |
| 401 | 抛出错误。请检查 TRENDSAPI_KEY |
| 404 | 抛出错误。没有该关键词的时间序列。请勿重试 |
| 429 | 抛出错误。超出配额 |
| 5xx | 客户端重试后抛出错误 |
HTTP body 字段是 JSON 字符串。SDK 会对其解码。使用原始 curl 时,必须再次解析 body。
网站:https://trendsapi.ai/trends/news-sentiment。
许可证
MIT。请参阅 LICENSE。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。