微算力 · API 导航与自营网关
API 接入 EN
未登录登录
📄

企业微信文档一键转 Markdown

WeChat Work Doc Fetcher · v1.0.0
办公 安全 B 级 作者 mouzhi 公开目录收录
1.2k 下载 0 安装 4.5k 收藏 69 浏览

简介

企业微信开发者文档一键抓取为 Obsidian Markdown,自动处理动态渲染与格式清理

原始描述

Fetch and convert WeChat Work developer docs pages into clean Markdown files for use in Obsidian, handling SPA content and required authentication.

通过你的 Agent 添加

点「快速安装」复制一段指令,粘给你正在用的 Agent(Claude Code、Codex、OpenClaw 等),它会自己完成安装。

或者手动安装

Claude Codeunzip -o skill.zip -d ~/.claude/skills/wechat-work-doc-fetcher/
其它 Agent解压到它自己的 skills 目录,目录名用 wechat-work-doc-fetcher

包里是 SKILL.md 加作者附带的文件,解压后目录名就是技能名。 技能申请的权限由作者决定,装之前请确认它能读什么、能执行什么。

中文使用说明 来自收录源

核心用法

wecom-doc-fetcher 是一个 Python 脚本工具,用于将企业微信(WeChat Work)开发者文档站点(developer.work.weixin.qq.com)的任意页面抓取并转换为干净的 Markdown 文件,便于导入 Obsidian 或其他笔记工具。

工作流程

  1. URL 解析:接收目标文档 URL(如 /path/94677)
  2. doc_id 获取:通过 Playwright 启动无头 Chromium,拦截页面 XHR 请求,自动提取 API 所需的 doc_id
  3. API 调用:向 /docFetch/fetchCnt 发送 POST 请求,获取 data.content_md 原始内容
  4. 格式清理:执行 11 项清洗规则(修复标题格式、移除 TOC 标记、转换 HTML 标签、处理表格间距等)
  5. 文件输出:写入指定路径的 .md 文件

显著优点

  • 精准提取:直接调用内部 API 获取 content_md,相比浏览器复制或 HTML 解析,保留完整格式且 token 效率最优
  • 自动化程度高:无需手动查找 doc_id,Playwright 自动完成浏览器会话与请求拦截
  • Obsidian 兼容:针对性修复表格前空行、HTML 标签转换等,确保在 Obsidian 中正确渲染
  • 灵活配置:支持手动指定 doc_id、运行时覆盖 cookies、输出到 stdout 或文件

潜在缺点与局限性

  • 依赖 Playwright:需下载约 150MB Chromium 二进制,首次配置较重
  • 会话时效性:wwapidoc.sid 有效期约 24 小时,wwapidoc.token_wt 约 30 分钟,过期需重新获取 cookies
  • API 非公开:依赖企业微信内部 API 结构,若官方变更端点或加密参数,工具可能失效
  • Python 环境限制:需 Python 3.8+,对非技术用户有一定门槛

适合人群

  • 企业微信生态开发者,需离线查阅或笔记化管理官方文档
  • 技术写作者,希望将 API 文档纳入个人知识库(Obsidian/Notion 等)
  • 自动化工作流用户,希望将文档更新纳入 CI/CD 或定时同步任务

常规风险

  • 账户安全:需提取浏览器 cookies 进行认证,存在会话泄露风险;建议定期轮换,避免长期存储
  • 服务稳定性:非官方支持工具,API 变更或反爬策略升级可能导致功能中断
  • 内容完整性:极端情况下 content_md 字段可能缺失或格式异常,需人工校验关键文档

技能正文

wecom-doc-fetcher

Use this skill when the user wants to save any page from the WeChat Work (企业微信) developer documentation site (developer.work.weixin.qq.com/document/path/*) as a clean Markdown file in their Obsidian vault.

Files in this skill

wecom-doc-fetcher/
├── SKILL.md          # this file
└── wx_doc_fetch.py   # the fetch & convert script

Setup (one-time)

Run these once before using the skill:

pip install requests playwright
playwright install chromium

playwright install chromium downloads a ~150 MB headless Chromium binary. This is required for automatic doc_id detection.

Python 3.8+ is required.


Usage

Place wx_doc_fetch.py anywhere convenient (e.g. your vault's scripts folder), then run:

# Basic: auto-detect doc_id, print to stdout
python wx_doc_fetch.py <URL>

# Save to file
python wx_doc_fetch.py <URL> output.md

# Skip Playwright, supply doc_id manually
python wx_doc_fetch.py <URL> output.md --doc-id <integer>

# Override cookies at runtime
python wx_doc_fetch.py <URL> output.md --cookies "wwapidoc.sid=xxx; ..."

Example

python wx_doc_fetch.py https://developer.work.weixin.qq.com/document/path/94677 发送消息.md
# [info] path_id=94677  doc_id=31152
# [done] 已写入:发送消息.md

How It Works

The WeChat Work docs site is a Vue SPA — the visible content is not in the initial HTML. It is loaded at runtime via a private POST API:

POST https://developer.work.weixin.qq.com/docFetch/fetchCnt?lang=zh_CN&ajax=1&f=json
Body: doc_id=<integer>   (application/x-www-form-urlencoded)

The response includes data.content_md — the page content as a Markdown string. The script fetches this field, cleans it, and writes the result.

Why not WebFetch / defuddle?

The page renders client-side. WebFetch and defuddle only see the pre-JS HTML skeleton — no content. Scraping innerText via browser tools works but produces a very large accessibility tree with poor formatting. The content_md API field is the cleanest, most token-efficient source.

URL path ID ≠ doc_id

The number in the browser URL (e.g. 94677) is a routing slug — not the doc_id the API needs. The actual doc_id (e.g. 31152) is determined at runtime by loading the page with Playwright and intercepting the fetchCnt XHR request.


Manual doc_id Fallback

If Playwright is unavailable or times out:

  1. Open the target URL in Chrome
  2. DevTools → Network tab → filter by fetchCnt
  3. Click the request → Payload tab
  4. Read the doc_id value
  5. Pass it with --doc-id:
python wx_doc_fetch.py https://developer.work.weixin.qq.com/document/path/94677 发送消息.md --doc-id 31152

Cookie Configuration

The fetchCnt API requires an authenticated session. Playwright's headless browser obtains session cookies automatically when loading the page — no manual cookie setup needed for normal use.

If you see errCode: -30001 in the output, the session is rejected. Fix:

  1. Open the site in Chrome while logged in
  2. DevTools → Network → any fetchCnt request → Copy as cURL
  3. Find the -b '...' cookie string in the copied command
  4. Either paste it into COOKIES_RAW at the top of wx_doc_fetch.py, or pass it via --cookies "..."

Key cookies and their lifetimes:

Cookie Purpose Lifetime
wwapidoc.sid Session identifier ~24 hours
wwapidoc.token_wt JWT auth token ~30 minutes

API Reference

Item Detail
Endpoint POST /docFetch/fetchCnt?lang=zh_CN&ajax=1&f=json&random=<rand>
Body doc_id=<integer> (form-urlencoded)
Auth Session cookies
Key response field data.content_md
Other response fields data.content_html, data.content_html_v2, data.content_txt, data.title, data.time

content_md Cleaning Rules

The content_md field is mostly valid CommonMark but has site-specific issues. The clean_md() function in wx_doc_fetch.py handles all of them:

# Problem Raw example After cleaning
1 [TOC] marker at top [TOC]\n# 概述 # 概述
2 Heading missing space after # ##接口定义 ## 接口定义
3 Internal numeric anchor links [接收事件](#12977) 接收事件
3 Anchors with sub-path [开启API](#31106/如何开启API) 开启API
4 HTML line breaks inside table cells 说明</br>补充 说明 补充
5 <b> bold tags <b>注意</b> **注意**
6 <code> inline tags <code>open_kfid</code> `open_kfid`
7 <font> color tags <font color="red">警告</font> 警告
8 !!#rrggbb text!! site-specific highlight !!#ff0000 重要!! 重要
9 Leading spaces before table rows ··| 参数 | | 参数 |
10 No blank line before table (Obsidian won't render) 文字\n| col | 文字\n\n| col |
11 Excess blank lines 3+ \n in a row 2 \n max

Rule 10 — critical regex note

The blank-line-before-table rule must match on lines that don't start with |, not just on the trailing character of the previous line:

# CORRECT — matches on start of line, avoids breaking table rows apart
re.sub(r"^([^|\n][^\n]*)\n(\|)", r"\1\n\n\2", content, flags=re.MULTILINE)

# WRONG — table rows end with "| " (trailing space), so last char is space,
#          causing blank lines to be inserted between every table row
re.sub(r"([^\n])\n(\|)", r"\1\n\n\2", content)

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

标签

wechat-workdocumentationobsidianmarkdownweb-scrapingapipythonplaywrightdeveloper-tools

同类 Skill