当你需要在应用中添加新闻搜索功能时,眼下要做的就是在不自行构建搜索流程的情况下,将有用的结果接入代码。读完本指南后,你将能够调用 News API、在 Python 中读取返回的新闻字段,并通过兼容 MCP 的 AI 智能体使用同一集成。
News API 返回的内容
News API 提供一个 Search News 端点,接收搜索词并返回新闻结果。返回的数据包含搜索参数,以及一个 news 数组,其中包含标题、链接、摘要、日期、来源、图片 URL 和排名。
因此,该端点适用于新闻搜索界面、主题监控仪表板,或需要获取相关报道链接的智能体。结果摘要是搜索结果文本,因此应用应将读者引导至来源,而不是把摘要当作完整文章呈现。
在 Zyla API Hub 上开始使用
打开 Zyla API Hub 上的 News API 页面;如果你还没有免费账户,请先创建一个,然后在可用时点击 Subscribe 或 Start Free Trial。从 Hub 获取 API 密钥,并在 Authorization 标头中将其用作 Bearer 令牌。
Zyla 采用带配额的订阅模式,而非按调用次数计费。首次 API 试用受 7 天或 50 次请求的限制;请查看 API 页面,了解当前的访问选项和价格。请将密钥保存在服务器或本地环境中,不要将其嵌入浏览器代码。
使用 GET 请求搜索新闻
可用端点为 Search News。它会搜索 q 中提供的词语。
方法: GET
请求 URL: https://zylalabs.com/api/2088/news-api/8710/search-news
q是必填字符串,包含搜索词。下面的请求使用USA。gl是必填字符串。下面的请求使用us。num是可选字符串。下面的请求使用50。
将 YOUR_API_KEY 替换为你的 Hub 密钥,然后运行此请求:
curl -s -X GET "https://zylalabs.com/api/2088/news-api/8710/search-news?q=USA&gl=us&num=50" \
-H "Authorization: Bearer YOUR_API_KEY"
以下是提供的响应示例。其搜索词为 frauds and scams,因此它展示的是响应字段,而不是上面 USA 请求的确切结果。提供的示例在某条结果中途结束,因此这里只展示节选,并非可完整解析的 JSON。
{"searchParameters":{"q":"frauds and scams","gl":"us","type":"news","engine":"google"},"news":[{"title":"Crypto Scam Tracker","link":"https://dfpi.ca.gov/consumers/crypto/crypto-scam-tracker/","snippet":"Crypto Blackmail Scam – Scammer sends emails or physical mails to victims saying they have personal information about the victim or embarrassing...","date":"3 days ago","source":"CA.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgiicmdcYcwifMoZJo-zKUAlcEIOZCvDl1-wz0dAAJigCnwL1z3Ty3m8o1fw&s","position":1},{"title":"Common Frauds and Scams | Federal Bureau of Investigation","link":"https://www.fbi.gov/how-we-can-help-you/scams-and-safety/common-frauds-and-scams","snippet":"Learn about some of the most common frauds and scams that the FBI encounters—as well as tips to help you stay safe and what to do if you're...","date":"7 months ago","source":"FBI.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKdrUO5MRmVg0GI7z198fT6rtJ9ylsRFmKjXykDrvrzaC4wQILPvbvtMqikg&s","position":2},{"title":"Bank transfer fraud losses outpace crypto","link":"https://www.paymentsdive.com/news/bank-transfer-fraud-losses-crypto-federal-trade-commission/743202/","snippet":"The value of fraud losses last year was highest for bank transfers and payments, followed by cryptocurrency transactions, according to a...","date":"1 day ago","source":"Payments Dive","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSj9XxTsKfvCq3xM6xpFGVIXaL9i09IEtjl_EKOBwt2lgDXfA6K1eccwZdTwQ&s","position":3},{"title":"New FTC Data Show a Big Jump in Reported Losses to Fraud to $12.5 Billion in 2024","link":"https://www.ftc.gov/news-events/news/press-releases/2025/03/new-ftc-data-show-big-jump-reported-losses-fraud-125-billion-2024","snippet":"Newly released Federal Trade Commission data show that consumers reported losing more than $12.5 billion to fraud in 2024, which represents...","date":"2 weeks ago","source":"Federal Trade Commission (.gov)","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR5Bw4fovNk1XEEElT5Y6JAs56siR5QuVXvijYIBxwSyRuUC0Mk2lBN9ajnLg&s","position":4},{"title":"Fraud Prevention, Worker Protection, and Fighting Alleged Big Tech…","link":"https://www.kelleydrye.com/viewpoints/blogs/ad-law-access/fraud-prevention-worker-protection-and-fighting-alleged-big-tech-censorship-a-roundup-of-the-new-ftcs-agenda-after-the-first-50-days","snippet":"So far, the FTC has issued several business blog posts, formed two new task forces…
对于结果列表,可使用 title 和 link 作为标题,并将 source、date 和 snippet 作为辅助文本。示例中的 date 值是诸如 3 days ago 的相对时间字符串;不要将其视为精确时间戳,也不要假定其时区。position 记录结果排名,而 imageUrl 可提供图片引用。
从 Python 调用端点
此示例使用 Python 标准库,发送与 curl 示例相同的请求,并打印每条返回新闻的字段。运行前,请在环境中设置 YOUR_API_KEY。
import json
import os
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
url = "https://zylalabs.com/api/2088/news-api/8710/search-news?q=USA&gl=us&num=50"
request = Request(
url,
headers={"Authorization": f"Bearer {os.environ['YOUR_API_KEY']}"},
method="GET",
)
try:
with urlopen(request, timeout=30) as response:
data = json.load(response)
for item in data.get("news", []):
print(item.get("title", ""))
print(item.get("source", ""), item.get("date", ""))
print(item.get("link", ""))
print(item.get("snippet", ""))
print()
except HTTPError as exc:
print(f"News API request failed: HTTP {exc.code}")
except URLError as exc:
print(f"Could not reach News API: {exc.reason}")
该脚本读取实时响应,而不是上面的截断示例。显示各个字段时,它使用 get,因此缺少某个显示值不会导致整个结果列表处理中断。在面向用户的应用中,还应在渲染 HTML 时转义返回的文本,并将返回的链接视为外部链接。
如果要缓存搜索结果,请将搜索词和其他请求参数纳入缓存键。根据应用需求选择刷新策略:缓存的相对日期仍是获取时返回的文本,并不会持续更新为时间戳。
在应用或 AI 智能体中使用结果
主题监控仪表板可以针对某个主题运行搜索,并显示返回的标题、来源和链接以供查看。新闻搜索页面可以使用相同字段构建结果列表;如果要保留响应顺序,请保留返回的排名。无论哪种情况,都应妥善处理空的 news 列表,不要将其视为请求失败。
对于 AI 工作流,请将兼容客户端连接到 https://mcp.zylalabs.com/mcp?apikey=YOUR_API_KEY。Zyla 的 MCP 端点可与 Claude Code、Cursor、Windsurf 及其他兼容 MCP 的客户端配合使用。
连接后,智能体可以使用 Hub 集成搜索新闻,并处理返回的标题、摘要、来源和链接。请要求它在输出中保留来源链接,以便读者查看相关报道原文;同时不要将 API 密钥放入提示词或共享的对话记录中。
常见问题
搜索新闻需要哪些参数?
q 和 gl 是必填字符串。num 是可选参数;文档中的请求使用 q=USA、gl=us 和 num=50。
响应中包含文章链接吗?
包含。提供的响应中,每条展示完整的新闻结果都包含一个 link,以及标题、摘要、来源和相对日期。
可以将返回的日期用作精确的发布时间戳吗?
根据提供的示例,不可以。日期是相对时间文本,因此应按原样显示,不要假定其对应精确日期或时区。
AI 智能体可以调用 News API 吗?
可以。使用你的 API 密钥将兼容 MCP 的客户端连接到 Zyla 的 MCP 端点,然后使用 Hub 集成执行新闻搜索工作流。
创建免费的 Zyla 账户,然后开始 News API 的 7 天试用,获取 API 密钥并查看当前的试用和订阅选项。
官方 API 响应
{"searchParameters":{"q":"frauds and scams","gl":"us","type":"news","engine":"google"},"news":[{"title":"Crypto Scam Tracker","link":"https://dfpi.ca.gov/consumers/crypto/crypto-scam-tracker/","snippet":"Crypto Blackmail Scam – Scammer sends emails or physical mails to victims saying they have personal information about the victim or embarrassing...","date":"3 days ago","source":"CA.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgiicmdcYcwifMoZJo-zKUAlcEIOZCvDl1-wz0dAAJigCnwL1z3Ty3m8o1fw&s","position":1},{"title":"Common Frauds and Scams | Federal Bureau of Investigation","link":"https://www.fbi.gov/how-we-can-help-you/scams-and-safety/common-frauds-and-scams","snippet":"Learn about some of the most common frauds and scams that the FBI encounters—as well as tips to help you stay safe and what to do if you're...","date":"7 months ago","source":"FBI.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKdrUO5MRmVg0GI7z198fT6rtJ9ylsRFmKjXykDrvrzaC4wQILPvbvtMqikg&s","position":2},{"title":"Bank transfer fraud losses outpace crypto","link":"https://www.paymentsdive.com/news/bank-transfer-fraud-losses-crypto-federal-trade-commission/743202/","snippet":"The value of fraud losses last year was highest for bank transfers and payments, followed by cryptocurrency transactions, according to a...","date":"1 day ago","source":"Payments Dive","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSj9XxTsKfvCq3xM6xpFGVIXaL9i09IEtjl_EKOBwt2lgDXfA6K1eccwZdTwQ&s","position":3},{"title":"New FTC Data Show a Big Jump in Reported Losses to Fraud to $12.5 Billion in 2024","link":"https://www.ftc.gov/news-events/news/press-releases/2025/03/new-ftc-data-show-big-jump-reported-losses-fraud-125-billion-2024","snippet":"Newly released Federal Trade Commission data show that consumers reported losing more than $12.5 billion to fraud in 2024, which represents...","date":"2 weeks ago","source":"Federal Trade Commission (.gov)","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR5Bw4fovNk1XEEElT5Y6JAs56siR5QuVXvijYIBxwSyRuUC0Mk2lBN9ajnLg&s","position":4},{"title":"Fraud Prevention, Worker Protection, and Fighting Alleged Big Tech…","link":"https://www.kelleydrye.com/viewpoints/blogs/ad-law-access/fraud-prevention-worker-protection-and-fighting-alleged-big-tech-censorship-a-roundup-of-the-new-ftcs-agenda-after-the-first-50-days","snippet":"So far, the FTC has issued several business blog posts, formed two new task forces…
官方 API 响应
{"searchParameters":{"q":"frauds and scams","gl":"us","type":"news","engine":"google"},"news":[{"title":"Crypto Scam Tracker","link":"https://dfpi.ca.gov/consumers/crypto/crypto-scam-tracker/","snippet":"Crypto Blackmail Scam – Scammer sends emails or physical mails to victims saying they have personal information about the victim or embarrassing...","date":"3 days ago","source":"CA.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgiicmdcYcwifMoZJo-zKUAlcEIOZCvDl1-wz0dAAJigCnwL1z3Ty3m8o1fw&s","position":1},{"title":"Common Frauds and Scams | Federal Bureau of Investigation","link":"https://www.fbi.gov/how-we-can-help-you/scams-and-safety/common-frauds-and-scams","snippet":"Learn about some of the most common frauds and scams that the FBI encounters—as well as tips to help you stay safe and what to do if you're...","date":"7 months ago","source":"FBI.gov","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKdrUO5MRmVg0GI7z198fT6rtJ9ylsRFmKjXykDrvrzaC4wQILPvbvtMqikg&s","position":2},{"title":"Bank transfer fraud losses outpace crypto","link":"https://www.paymentsdive.com/news/bank-transfer-fraud-losses-crypto-federal-trade-commission/743202/","snippet":"The value of fraud losses last year was highest for bank transfers and payments, followed by cryptocurrency transactions, according to a...","date":"1 day ago","source":"Payments Dive","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSj9XxTsKfvCq3xM6xpFGVIXaL9i09IEtjl_EKOBwt2lgDXfA6K1eccwZdTwQ&s","position":3},{"title":"New FTC Data Show a Big Jump in Reported Losses to Fraud to $12.5 Billion in 2024","link":"https://www.ftc.gov/news-events/news/press-releases/2025/03/new-ftc-data-show-big-jump-reported-losses-fraud-125-billion-2024","snippet":"Newly released Federal Trade Commission data show that consumers reported losing more than $12.5 billion to fraud in 2024, which represents...","date":"2 weeks ago","source":"Federal Trade Commission (.gov)","imageUrl":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR5Bw4fovNk1XEEElT5Y6JAs56siR5QuVXvijYIBxwSyRuUC0Mk2lBN9ajnLg&s","position":4},{"title":"Fraud Prevention, Worker Protection, and Fighting Alleged Big Tech…","link":"https://www.kelleydrye.com/viewpoints/blogs/ad-law-access/fraud-prevention-worker-protection-and-fighting-alleged-big-tech-censorship-a-roundup-of-the-new-ftcs-agenda-after-the-first-50-days","snippet":"So far, the FTC has issued several business blog posts, formed two new task forces…