关于 zhihu
zhihu 是用于搜索、阅读和发布知乎内容的技能,面向管理个人知乎内容及检索站内外信息的用户。它通过独立脚本和两种凭据区分搜索与账号内容操作。
主要功能
- 01搜索知乎内容和全网网页
- 02查看知乎热榜
- 03读取个人文章与回答及统计
- 04发布文章或回答并编辑回答
- 05写入操作支持预览及草稿
适用场景
- 01查找知乎问题、回答和文章
- 02查看热门话题
- 03检查个人内容及互动统计
- 04将文章或回答保存为草稿或发布
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ acedatacloud/ skills --skill zhihu安装后在 Claude Code 中输入/zhihu确认已加载。
附带文件scripts/blog.pyscripts/search.py
详细介绍
zhihu — 在知乎上搜索、阅读和发布
两种身份验证方式,两个脚本:
| 脚本 | 身份验证 | 功能 |
|---|---|---|
scripts/search.py | ZHIHU_DEVELOPER_TOKEN(Bearer) | 搜索知乎、搜索网页、获取热门话题 |
scripts/blog.py | ZHIHU_COOKIES(登录 Cookie) | 阅读/撰写自己的文章和回答 |
无需浏览器,也无需第三方依赖——仅使用 urllib。
连接器会将凭据注入为环境变量:
ZHIHU_DEVELOPER_TOKEN— 知乎开发者平台访问密钥(Bearer 令牌)。 用于搜索和热榜查询。机密信息——切勿回显或打印。ZHIHU_COOKIES—{name, value, domain, path, ...}Cookie 对象组成的 JSON 数组。 用于读写用户自己的内容。机密信息——切勿回显或打印。
先定位脚本(每个 Bash 代码块都要执行)
连接器会将 $SKILL_DIR 设置为此技能的目录,因此脚本位于
$SKILL_DIR/scripts/。不要直接硬编码 python3 $SKILL_DIR/scripts/…。
如果同一轮加载了多个技能,$SKILL_DIR 可能指向
其他技能,导致调用失败并报错 No such file or directory(这是知乎操作悄无声息地未完成的首要原因)。请在每个 Bash 代码块开头防御性地解析路径——每次 Bash 调用都会启动一个新 shell,因此变量不会延续:
# blog.py is unique to this skill, so it anchors zhihu's dir even if $SKILL_DIR is wrong.
ZDIR="$SKILL_DIR"; [ -f "$ZDIR/scripts/blog.py" ] || ZDIR=$(find /tmp -maxdepth 8 -path '*/skills/*/scripts/blog.py' 2>/dev/null | head -1 | sed 's#/scripts/blog.py##')
[ -f "$ZDIR/scripts/blog.py" ] || { echo "zhihu scripts not found (SKILL_DIR=$SKILL_DIR) — is the skill loaded?" >&2; exit 1; }
SEARCH="$ZDIR/scripts/search.py"; BLOG="$ZDIR/scripts/blog.py"
然后按下文所示使用 "$SEARCH" / "$BLOG"(始终加引号)。
搜索 CLI(search.py)
scripts/search.py — 搜索知乎和网页。仅需
ZHIHU_DEVELOPER_TOKEN(无需 Cookie)。
# Resolve the scripts first (see "Locate the scripts first" above) — robust to $SKILL_DIR.
ZDIR="$SKILL_DIR"; [ -f "$ZDIR/scripts/blog.py" ] || ZDIR=$(find /tmp -maxdepth 8 -path '*/skills/*/scripts/blog.py' 2>/dev/null | head -1 | sed 's#/scripts/blog.py##')
SEARCH="$ZDIR/scripts/search.py"
# Search Zhihu content (站内搜索) — questions, answers, articles
python3 "$SEARCH" search "Python 爬虫"
python3 "$SEARCH" search "Python 爬虫" --count 5
# Search the entire web (全网搜索) — all indexed sites
python3 "$SEARCH" global "AI Agent"
python3 "$SEARCH" global "AI Agent" --count 15
# Filter by site or time
python3 "$SEARCH" global "React" --filter 'host=="github.com"'
python3 "$SEARCH" global "新闻" --filter 'publish_time>=1720000000'
python3 "$SEARCH" global "技术" --filter 'host=="github.com" AND publish_time>=1720000000'
python3 "$SEARCH" global "实时新闻" --db realtime
# Get Zhihu trending topics (热榜)
python3 "$SEARCH" hot
python3 "$SEARCH" hot --limit 10
搜索命令
| 目标 | 命令 |
|---|---|
| 搜索知乎(最多 10 条结果) | python3 "$SEARCH" search "<query>" --count N |
| 搜索整个网页(最多 20 条) | python3 "$SEARCH" global "<query>" --count N |
| 按网站筛选 | --filter 'host=="example.com"' |
| 按时间筛选 | --filter 'publish_time>=<unix_ts>' |
| 仅搜索实时/静态索引 | --db realtime 或 --db static |
| 知乎热门话题(最多 30 条) | python3 "$SEARCH" hot --limit N |
搜索结果字段
zhihu_search 返回:标题、类型(文章/回答)、content_id、url、 摘要、vote_up(赞同数)、评论数、作者、权威等级、edit_time。
global_search 返回相同字段,另附 has_more 指示。url
包含知乎平台添加的 utm 跟踪参数。
hot_list 返回:排名、标题、url、摘要、缩略图。
global_search 筛选语法
host=="example.com"— 按域名筛选(注意:不支持host=="zhihu.com",请改用search命令)publish_time>=1720000000— 按发布时间筛选(Unix 秒)- 逻辑运算符:
AND、OR(必须大写) - 使用括号分组:
(host=="a.com" OR host=="b.com") AND publish_time>=T
博客 CLI(blog.py)
此技能提供 scripts/blog.py — 独立脚本,仅使用标准库。
需要 ZHIHU_COOKIES(登录 Cookie)。
# Resolve the scripts first (see "Locate the scripts first" above) — robust to $SKILL_DIR.
ZDIR="$SKILL_DIR"; [ -f "$ZDIR/scripts/blog.py" ] || ZDIR=$(find /tmp -maxdepth 8 -path '*/skills/*/scripts/blog.py' 2>/dev/null | head -1 | sed 's#/scripts/blog.py##')
BLOG="$ZDIR/scripts/blog.py"
# Read (run directly)
python3 "$BLOG" whoami # who is logged in
python3 "$BLOG" articles --limit 20 # my published articles + stats
python3 "$BLOG" article <article-id> # one article's details + stats
python3 "$BLOG" answers --limit 20 # my published answers + stats
python3 "$BLOG" answer <answer-id> # one answer's details + stats
python3 "$BLOG" question <question-id> # a question's info + whether I answered it
先验证连接
ZDIR="$SKILL_DIR"; [ -f "$ZDIR/scripts/blog.py" ] || ZDIR=$(find /tmp -maxdepth 8 -path '*/skills/*/scripts/blog.py' 2>/dev/null | head -1 | sed 's#/scripts/blog.py##')
python3 "$ZDIR/scripts/blog.py" whoami
# → {"id": "...", "name": "<the connected display name>", "url_token": "<the connected handle>", ...}
如果返回 401/403,说明 Cookie 已过期——请告知用户前往
https://studio.acedata.cloud/console/connectors 重新连接(使用 ACE
扩展程序重新捕获)。不要循环重试。
阅读配方
| 目标 | 命令 |
|---|---|
| 我是谁 | python3 "$BLOG" whoami |
| 我最新的文章 + 赞同/评论数 | python3 "$BLOG" articles --limit 20 |
| 我最新的回答 + 喜欢/收藏/评论数 | python3 "$BLOG" answers --limit 20 |
| 下一页(任意列表) | 添加 --offset 20 |
| 单篇文章的数据 | python3 "$BLOG" article <id> |
| 单个回答的数据(包括赞同数) | python3 "$BLOG" answer <id> |
| 问题信息 + 我的回答 ID(如有) | python3 "$BLOG" question <id> |
文章数据:voteup_count(赞同)、comment_count(评论)。知乎不会在这些端点公开单篇文章的阅读数。
回答数据注意事项:answers 列表端点不会返回 voteup_count(赞同),只提供 like_count(喜欢)、favorite_count(收藏)和 comment_count。若要获取回答的权威赞同数,请调用 answer <id>(单个回答端点会返回 voteup_count)。question <id> 会报告 already_answered + my_answer_id,据此可判断应使用 answer-question(新建)还是 edit-answer(更新)。
发布——需确认(除非末尾带 --confirm,否则为试运行)
publish 会写入用户的真实账号。不带末尾的 --confirm 时会进行试运行(打印将执行的操作,不做任何更改)。只有当 --confirm 是最后一个参数时才会生效,因此标题/内容中包含 "--confirm" 绝不会意外发布。务必先向用户展示试运行结果,取得明确的“是”后,再在末尾带上 --confirm 重新运行。
# Content is HTML. For Markdown, convert to HTML first (e.g. `pandoc -f gfm -t html`).
python3 "$BLOG" publish --title "标题" --content-file article.html # dry-run
python3 "$BLOG" publish --title "标题" --content-file article.html --draft-only --confirm # save a private draft
python3 "$BLOG" publish --title "标题" --content-file article.html --confirm # PUBLIC, goes live
--draft-only会在保存为私人草稿后停止(安全——不会公开)。- 不使用
--draft-only时,文章会以用户的名义公开发布。除非用户明确要求立即发布,否则默认使用--draft-only。 - **图片会自动托管。**知乎会移除
src不在其自有 CDN 上的任何<img>,因此使用--confirm时,CLI 会将每张外部图片(HTML<img src>、Markdown,以及data:URI)重新上传到知乎的图片服务,并先改写 URL——已托管在*.zhimg.com上的图片不会更改。结果会报告images: {found, rehosted, failed};试运行会报告images_found。传入--no-images可跳过此操作。因此,你可以将包含常规公开图片 URL 的 HTML/Markdown 交给 CLI,图片也能正常显示。
回答问题——需确认(除非末尾带 --confirm,否则为试运行)
两个写入命令涵盖问题/回答相关操作。两者的确认机制与 publish 完全相同:末尾不带 --confirm → 试运行;只有当 --confirm 是最后一个参数时才会生效。务必先展示试运行结果,取得明确的“是”后,再在末尾带上 --confirm 重新运行。
# Content is HTML (same as articles). For Markdown, convert to HTML first.
# Post a NEW answer to a question
python3 "$BLOG" answer-question --question <qid> --content-file ans.html # dry-run
python3 "$BLOG" answer-question --question <qid> --content-file ans.html --draft-only --confirm # PRIVATE draft (safe)
python3 "$BLOG" answer-question --question <qid> --content-file ans.html --confirm # PUBLIC, goes live
# Edit an EXISTING answer (replaces its live, public content)
python3 "$BLOG" edit-answer --id <answer-id> --content-file ans.html # dry-run
python3 "$BLOG" edit-answer --id <answer-id> --content-file ans.html --confirm # overwrites live answer
- **每个问题只能有一个回答。**知乎允许每位用户对每个问题只发布一个回答。如果用户已经回答过,
answer-question --confirm会返回明确错误,提示改用edit-answer——可通过answers或question <qid>(会报告my_answer_id)查找已有回答 ID。 - 新回答使用
--draft-only是安全做法——这会在问题下保存一份私人草稿(不会公开)。用户在知乎上审核后,你再不带--draft-only(但带--confirm)重新运行以发布。除非用户明确要求立即发布,否则优先采用此做法。 edit-answer没有私人模式——回答已经公开,因此任何带--confirm的编辑都会立即生效。除非使用--repost allowed|disallowed覆盖设置,否则会保留回答当前的转载设置。- 转载授权(
--repost allowed|disallowed):新回答默认设为disallowed(不授予转载权);编辑时保留当前设置。 - 回答中的图片也会自动托管——行为与
publish相同,且使用相同的--no-images标记。
注意事项——提前告知,避免用户措手不及
- 这是用户的真实知乎账号。 发布、回答或编辑前务必确认;读取内容会暴露用户自己的私人草稿。
- Cookie 过期:知乎 Cookie 的有效期较短。遇到
401/403时,表示需要在 studio.acedata.cloud/console/connectors 重新连接——切勿循环重试。 - 服务条款:使用 Cookie 自动化操作违反大多数平台的服务条款。本工具仅使用用户自己的 Cookie 操作用户自己的账号;相关风险由用户承担。切勿用它批量抓取他人内容。
- 切勿打印
ZHIHU_COOKIES——它拥有完整的账号访问权限。 - 范围:仅限知乎。其他中国平台(掘金 / CSDN / …)会作为各自独立的平台技能发布(例如
csdn、juejin),每个技能都有自己的连接器——此处没有--platform开关。
记录输出
成功发布并获取有效结果 URL 后,调用内置的
publish_artifact 工具一次,让用户可以在 My Outputs 中跟踪此交付内容:
publish_artifact(kind="article", channel="zhihu", title="<title>", url="<the REAL returned URL>", status="delivered")
使用实际返回的 URL——切勿编造。每个已发布内容仅调用一次,且仅在确认交付后调用;如果发布失败,则跳过(或使用 status="failed")。
参见 _shared/artifacts.md。