# Zhihu User Archive

> 把知乎某个用户（以 token 标识）的**回答 / 文章 / 想法**全量归档成本地 Markdown，并支持后续**增量更新**——只补新内容、只重抓被编辑过的旧内容，输出可直接用编辑器或静态站点浏览。

Zhihu User Archive is a paid AI agent skill on Agensi by kevin. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $1 (one-time purchase)
- Creator: [kevin](https://www.agensi.io/creators/kevin-5)
- Version: 1.1.0
- Last updated: 2026-12-01
- Security: scan passed (2026-12-01); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/zhihu-user-archive

## About this skill

### zhihu-user-archive

把知乎某个用户（以 token 标识）的**回答 / 文章 / 想法**全量归档成本地 Markdown，并支持后续**增量更新**——只补新内容、只重抓被编辑过的旧内容，输出可直接用编辑器或静态站点浏览。

当前版本：**1.1.0**

---

#### 它解决什么问题

知乎没有内容导出功能，第三方工具大多只能抓一次。这个项目把「一次性全量爬取」升级成「可持续维护的本地副本」：

- 首次归档 → 完整落地正文、图片、公式，生成带目录的 `index.md`
- 之后每次更新 → 比对 ID **和更新时间**，新增的补抓、被静默编辑的旧内容重新拉取
- 全程自校验 → 图片引用数与磁盘文件数对不上、出现远端图链、公式残留，都会在最后一步暴露出来

---

#### 目录结构

```
zhihu-user-archive-skill/
├── SKILL.md                  # 技能定义 + 完整操作手册（含踩坑记录，最重要的一份文档）
├── README.md                 # 本文件
└── scripts/
    ├── refresh_ids.py        # 强制重新枚举 ID（增量前必经步骤）
    └── incremental_update.py # 增量更新总管：diff → 补抓 → 重建 → 校验
```

本仓库**只包含增量更新层**，不含抓取实现。真正干活的下层脚本位于每个归档项目的 `{project}/_tools/` 中：

| 脚本 | 职责 |
| --- | --- |
| `harvest_ids.py` | 枚举内容 ID（想法顺带落地 `content_pins/`） |
| `fetch_answers_list.py` | 走列表接口取正文（已存在文件自动跳过） |
| `fetch_details.py` | 补充详情（部分详情接口已被风控，见下） |
| `build_md.py` | HTML→Markdown、公式还原、图片落地、生成 `index.md` |

---

#### 环境要求

| 项 | 说明 |
| --- | --- |
| Python | 3.9+，需 `requests` / `beautifulsoup4` / `lxml` / `markdownify` |
| 解释器路径 | 脚本内硬编码为 `/home/kevin/.workbuddy/binaries/python/envs/default/bin/python`，换机器需修改 `PY` 常量 |
| 网络 | 能访问 （双通道轮换） |
| 凭证 | 登录态 Cookie，存于 `{project}/.secrets/cookie.txt` |

> ⚠️ 两个脚本都用 `find_root()` 从**当前工作目录**向上最多 6 层寻找含 `_raw/` 的目录作为项目根，因此**必须在归档项目目录下执行**，不能从 skill 目录直接跑。

---

#### 安装

作为技能使用，软链或复制到 WorkBuddy 技能目录即可：

```bash
ln -s ~/MyData/zhihu-user-archive-skill ~/.workbuddy/skills/zhihu-user-archive
```

之后在对话里说「归档这个知乎用户」「更新下载内容」「增量更新归档」即可触发。

---

#### 使用

##### 一、首次全量归档

1. **探测账号**（匿名可用，确认 token 与内容量）
   ```bash

   ```
2. **取 Cookie**：浏览器扫码登录后从 CDP 层提取（`z_c0` 是 httpOnly，`document.cookie` 读不到），整行 `k=v; k=v` 存入 `{project}/.secrets/cookie.txt`
3. **枚举 → 抓正文 → 构建**
   ```bash
   cd {project}/_tools
   python harvest_ids.py
   python fetch_answers_list.py
   python build_md.py
   ```

##### 二、增量更新

```bash
cd {project}/_tools

# 1. 强制重枚举（自动备份旧 ids，再全量拉一遍）
python ~/.workbuddy/skills/zhihu-user-archive/scripts/refresh_ids.py

# 2. 一步到底：diff → 补抓 → 移空输出 → 全量重建 → 校验
python ~/.workbuddy/skills/zhihu-user-archive/scripts/incremental_update.py

# 只看变更，不抓不建
python ~/.workbuddy/skills/zhihu-user-archive/scripts/incremental_update.py --diff

# 只做质量校验
python ~/.workbuddy/skills/zhihu-user-archive/scripts/incremental_update.py --check
```

---

#### 脚本说明

##### `refresh_ids.py`

| 参数 | 行为 |
| --- | --- |
| 无 | 把 `_raw/ids_{answers,articles,pins}.json` 移到 `_raw/ids_old_<时间戳>/`，再调用 `harvest_ids.py` 全量重枚举，最后打印各类条数；只保留最近 3 份备份 |
| `--keep` | 保留全部历史备份 |

**为什么必须强制重枚举**：`harvest_ids.py` 带断点续传，只要 `ids_*.json` 里 `done=true` 就直接打印「已完成 N 条，跳过」并退出。增量场景下直接跑它等于什么都没做。

##### `incremental_update.py`

| 参数 | 行为 |
| --- | --- |
| 无 | 完整流程：比对 → 删掉被编辑条目的旧正文 → 补抓 → 把 `answers/posts/pins` 移入 `_trash/` → 全量重建 → 校验 |
| `--diff` | 只打印 `+ 新增 / - 消失 / ~ 有编辑`，不动任何文件 |
| `--check` | 只跑质量校验，不抓不建 |

退出码非 0 表示流程中断，可直接用于 CI / 定时任务判断。

---

#### 几个关键设计

**比对 `updated`，不只比对 ID。** 作者编辑旧内容时不产生新 ID，只比 ID 会漏掉。实测中就是靠这一步发现某篇旧回答被改过。

**重建前必须把旧输出整体移走，不能原地重建。** 编号按发布时间倒序生成，新增一条会让后面所有编号位移：旧的 `0001-宁德时代.md` 还在，新的 `0001-德明利.md` 又写进来，数量直接翻倍。想靠「记录文件名集合、事后删差集」也不行——新旧 `0001` 文件名不同，两边都不在差集里。所以脚本统一 `mv` 到 `_trash/` 后全量重建。

**不要用 mtime 判断新旧。** `os.makedirs(exist_ok=True)` 不会更新已存在目录的 mtime，按 mtime 清理会误删**所有** `.assets/` 目录，图片全丢。

**账面数 ≠ 实抓数。** `answer_count` 有缓存延迟，已删除 / 折叠 / 私有内容接口不下发，差 1~2 条属正常，如实记录即可，不要反复重试追平。

**正文走列表接口。** `/answers/{id}` 和 `/articles/{id}` 详情接口已被风控（40362 / 10003），改用 `members/{token}/{answers|articles}?include=data[*].content`，并做截断自检（去标签后的正文长度不得短于 `excerpt`）。

---

#### 质量校验

`--check` 会检查五项，全部通过才返回成功：

- 图片引用数 == 磁盘实际文件数
- 悬空引用 0（引用的文件不存在）
- 远端图链 0（不允许 `http(s)://` 直链，必须本地化）
- `/equation` 图片残留 0（公式应还原成 `$$...$$`）
- 各目录编号连续且唯一

对照基线（2026-10-26 一次真实更新后测得）：207 回答 / 9 文章 / 20 想法；图片引用 50 : 磁盘 50；悬空 0、远端 0、公式残留 0。

---

#### 常见问题

| 现象 | 原因与处理 |
| --- | --- |
| 接口返回 602 / 40362 | Cookie 失效，重新扫码登录，不要反复重试 |
| 单页偶发 `SSLError` / `ReadTimeout` | 网络抖动，脚本自带双通道轮换 + 指数退避，会自动续上；连续 5~7 次失败才需冷却重跑 |
| 删除大量文件被拦截 | 安全策略单次限 50 个，且连续相同命令会被判循环；优先 `mv` 到 `_trash/`，或让用户手动清理 |
| `items[0]` 报 `KeyError: 0` | `_raw/ids_*.json` 的 `items` 是 **dict** 不是 list，用 `items[id]` 或 `items.values()` |
| 想法正文转换异常 | 想法 `content` 是结构化数组（text/image/link），不是 HTML，需单独处理 |
| `pip install` 卡住或失败 | `/tmp` 是 10M tmpfs，需 `TMPDIR=~/.workbuddy/tmp` 后重试 |

更多细节见 `SKILL.md`。

---

#### 版本

- **1.1.0** — 新增 `refresh_ids.py`、`incremental_update.py`，支持增量更新与自动校验
- **1.0.0** — 首次全量归档流程

---

#### 免责声明

本项目用于**个人内容备份**，仅能归档你有权访问的公开内容。使用时请遵守知乎用户协议与 `robots.txt`，控制请求频率（脚本内置 1~3s 随机间隔），不要用于批量采集他人数据或任何商业分发。归档内容版权归原作者所有。

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Zhihu User Archive skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### What does Zhihu User Archive do?

把知乎某个用户（以 token 标识）的**回答 / 文章 / 想法**全量归档成本地 Markdown，并支持后续**增量更新**——只补新内容、只重抓被编辑过的旧内容，输出可直接用编辑器或静态站点浏览。

### Which AI agents support Zhihu User Archive?

This skill uses the open SKILL.md standard. Use it through an MCP-compatible agent connected to Agensi, and check the compatibility information on this page for confirmed agents and required capabilities.

### How do I use Zhihu User Archive?

Connect your AI agent to Agensi through MCP, then ask it to use Zhihu User Archive on your task. Your agent loads the latest approved version each time. Paid skills require a purchase or active subscription. Skill files are not available for download; your licence does not allow you to save, copy or export any part of a skill.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
