知乎数据开放平台 API 技能
通过知乎官方开放平台(developer.zhihu.com)提供的 REST API,实现知乎搜索、全网搜索、知乎直答 AI 对话、知乎热榜获取等功能。
API 端点
| 功能 | 方法 | URL |
|------|------|-----|
| 知乎搜索 | GET | https://developer.zhihu.com/api/v1/content/zhihu_search |
| 全网搜索 | GET | https://developer.zhihu.com/api/v1/content/global_search |
| 知乎直答 | POST | https://developer.zhihu.com/v1/chat/completions |
| 知乎热榜 | GET | https://developer.zhihu.com/api/v1/content/hot_list |
鉴权方式
所有接口统一使用 Bearer 鉴权,需在请求头中携带:
Authorization: Bearer <your_access_secret>
X-Request-Timestamp: <unix_seconds>
Content-Type: application/json
Authorization:Bearer Token,填入 Access SecretX-Request-Timestamp:秒级 Unix 时间戳Content-Type:固定值application/json
Access Secret 获取
在知乎开放平台个人中心(https://developer.zhihu.com/profile)查看并获取 Access Secret。
接口详情
1. 知乎搜索
站内内容搜索,返回知乎上的问题、回答、文章。
请求参数(Query):
| 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | Query | String | 是 | 搜索关键词 | | Count | Int32 | 否 | 返回数量,默认 10,最大 10 |
Python 调用示例:
import requests, time
API_KEY = "<your_access_secret>"
def zhihu_search(query: str, count: int = 5):
"""知乎站内搜索"""
url = "https://developer.zhihu.com/api/v1/content/zhihu_search"
headers = {
"Authorization": f"Bearer {API_KEY}",
"X-Request-Timestamp": str(int(time.time())),
"Content-Type": "application/json",
}
params = {"Query": query, "Count": min(count, 10)}
r = requests.get(url, params=params, headers=headers, timeout=15)
r.raise_for_status()
data = r.json()
if data.get("Code") != 0:
raise Exception(f"API Error: {data.get('Message')}")
return data["Data"]
响应字段(Item):
| 字段 | 说明 | |------|------| | Title | 内容标题 | | ContentType | 内容类型(Answer/Article/Question) | | ContentID | 内容标识 | | ContentText | 内容摘要 | | Url | 内容链接 | | CommentCount | 评论数 | | VoteUpCount | 赞同数 | | AuthorName | 作者昵称 | | AuthorAvatar | 作者头像 URL | | AuthorBadgeText | 认证文案 | | EditTime | 发布/更新时间戳 | | AuthorityLevel | 权威等级(1-4) | | CommentInfoList | 精选评论 |
2. 全网搜索
全网内容搜索,支持按站点域名、发布时间等条件过滤。
请求参数(Query):
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| Query | String | 是 | 搜索关键词 |
| Count | Int32 | 否 | 返回数量,默认 10,最大 20 |
| Filter | String | 否 | 高级过滤表达式(需 URL 编码) |
| SearchDB | String | 否 | 索引库:all(默认)、realtime(实时)、static(静态) |
Filter 高级语法:
host=="example.com"— 按站点域名过滤publish_time>=1778494631— 按发布时间过滤(秒级时间戳)- 支持逻辑符:
AND、OR,可用()控制优先级 - 注意:
host=="zhihu.com"不支持,站内搜索请用zhihu_search
Python 调用示例:
def global_search(query: str, count: int = 10, filter: str = "", search_db: str = "all"):
"""全网搜索"""
url = "https://developer.zhihu.com/api/v1/content/global_search"
headers = {
"Authorization": f"Bearer {API_KEY}",
"X-Request-Timestamp": str(int(time.time())),
"Content-Type": "application/json",
}
params = {"Query": query, "Count": min(count, 20)}
if filter:
params["Filter"] = filter
if search_db:
params["SearchDB"] = search_db
r = requests.get(url, params=params, headers=headers, timeout=15)
r.raise_for_status()
data = r.json()
if data.get("Code") != 0:
raise Exception(f"API Error: {data.get('Message')}")
return data["Data"]
3. 知乎直答(AI 对话)
调用知乎直答 AI 模型进行对话问答,OpenAI SDK 兼容格式。
支持模型:
| 模型 | 说明 |
|------|------|
| zhida-fast-1p5 | 快速回答,轻量响应 |
| zhida-thinking-1p5 | 深度思考,含推理过程(reasoning_content) |
| zhida-agent | 智能思考,Agent 模式 |
请求体(JSON):
{
"model": "zhida-thinking-1p5",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好"}
],
"stream": false
}
Python 调用示例:
def zhida_chat(messages: list, model: str = "zhida-fast-1p5", stream: bool = False):
"""知乎直答 AI 对话"""
url = "https://developer.zhihu.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"X-Request-Timestamp": str(int(time.time())),
"Content-Type": "application/json",
}
body = {"model": model, "messages": messages, "stream": stream}
if stream:
r = requests.post(url, json=body, headers=headers, timeout=60, stream=True)
r.raise_for_status()
for line in r.iter_lines(decode_unicode=True):
if line and not line.startswith(":") and line != "data: [DONE]":
print(line)
else:
r = requests.post(url, json=body, headers=headers, timeout=60)
r.raise_for_status()
return r.json()
响应字段(非流式):
| 字段 | 说明 | |------|------| | choices[0].message.content | 回答内容 | | choices[0].message.reasoning_content | 推理过程(thinking 模型独有) |
注意: zhida-agent 模型仅支持单轮 messages(仅 user),不支持 system/assistant 上下文。zhida-fast-1p5 和 zhida-thinking-1p5 支持多轮上下文。
4. 知乎热榜
获取当前知乎热榜内容列表。
请求参数(Query):
| 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | Limit | Int32 | 否 | 返回数量,默认 30,最大 30 |
Python 调用示例:
def zhihu_hot_list(limit: int = 10):
"""获取知乎热榜"""
url = "https://developer.zhihu.com/api/v1/content/hot_list"
headers = {
"Authorization": f"Bearer {API_KEY}",
"X-Request-Timestamp": str(int(time.time())),
"Content-Type": "application/json",
}
params = {"Limit": min(limit, 30)}
r = requests.get(url, params=params, headers=headers, timeout=15)
r.raise_for_status()
data = r.json()
if data.get("Code") != 0:
raise Exception(f"API Error: {data.get('Message')}")
return data["Data"]
响应字段(Item):
| 字段 | 说明 | |------|------| | Title | 热榜标题 | | Url | 知乎链接 | | ThumbnailUrl | 缩略图 URL | | Summary | 内容摘要 |
错误码
| 错误码 | 说明 | |--------|------| | 0 | 成功 | | 10001 | 参数错误 | | 20001 | 鉴权失败 | | 30001 | 频率限制 | | 90001 | 内部错误 |
使用注意
- Access Secret 为敏感信息,Skill 中使用时通过环境变量或配置管理,不要硬编码在公开代码中
X-Request-Timestamp必须传当前秒级 Unix 时间戳- 所有请求需设置
Content-Type: application/json - 知乎搜索单次最多返回 10 条,全网搜索最多 20 条,热榜最多 30 条
- 全网搜索的
host过滤不支持zhihu.com及其子域名,站内搜索请直接使用zhihu_search
微信扫一扫