Specialized documentation website crawler optimized for AI consumption
Project description
DocsForAI
专为文档网站设计的轻量爬虫,让 AI 可以高质量地阅读任何文档。
为什么不用通用爬虫?
通用爬虫把所有网站一视同仁。而 VitePress、Docsify 等文档站有明确的结构约定:
| 特性 | 通用爬虫 | DocsForAI |
|---|---|---|
| 导航层级 | ❌ 需要猜 | ✅ 直接读侧边栏 / llms.txt |
| Docsify 原始 Markdown | ❌ 解析渲染后的 HTML | ✅ 直接拉取 .md 文件 |
| Mintlify 一次请求获全量内容 | ❌ 逐页爬取 | ✅ 解析 llms-full.txt 即完成 |
| 代码块语言标注 | ❌ 常丢失 | ✅ 保留 language-* 类名 |
| 输出格式 | ❌ 单一 | ✅ 多 MD / 单 MD / JSONL |
| 依赖 | 通常很重 | ✅ 仅 5 个运行时依赖 |
安装
pip install docsforai
或从源码安装:
git clone https://github.com/dx2331lxz/DocsForAI.git
cd DocsForAI
pip install -e .
快速开始
# 爬取 VitePress 文档,输出为多个 MD 文件(默认)
docsforai crawl https://vitepress.dev/guide -o ./output
# 输出为单一大文件(直接投喂给 LLM)
docsforai crawl https://vitepress.dev/guide -f single-md -o ./output
# 输出为 JSONL(适合向量数据库 / 微调数据集)
docsforai crawl https://docsify.js.org -f jsonl -o ./output
# 同时导出多种格式
docsforai crawl https://vitepress.dev/guide -f multi-md -f jsonl -o ./output
# 强制指定站点类型(跳过自动检测)
docsforai crawl https://example.com/docs --type vitepress
支持的站点类型
VitePress
- 自动识别
.VPSidebar结构,完整还原章节层级 - 提取
.vp-doc内容区,剔除导航栏、页脚等噪音 - 保留代码块的语言标注
Docsify
- 直接解析
_sidebar.md获取目录树 - 跳过 HTML 渲染,直接拉取原始
.md文件,速度最快、内容最准 - 正确处理哈希路由(
/#/guide→/guide.md)
Mintlify
- 通过响应头
x-llms-txt自动识别 - 优先读取
llms-full.txt:一次 HTTP 请求即获取所有页面的完整内容,无需遍历 - 回退到
llms.txt索引 + 并发拉取各页.md原始文件 - 零 HTML 解析,内容干净准确
Generic(通用兜底)
- BFS 广度优先遍历同域链接
- 启发式识别主内容区(
main、article、.content等) - 可通过
--max-pages限制爬取深度
Feishu (飞书开放平台)
- 专用爬虫:通过飞书开放平台暴露的内部 API 拉取完整的目录树和原始 Markdown(
/document/<fullpath>.md)。 - 优点:一次性读取目录树并并发下载所有
.md,内容干净且保留原始 Markdown 结构。
Docusaurus
- Docusaurus 是一个常见的文档框架;DocsForAI 当前通过
generic兜底爬虫兼容 Docusaurus 网站。 - 测试:已对
https://docusaurus.io/docs运行爬虫(docsforai crawl https://docusaurus.io/docs -o ./output_docusaurus),采集到 202 页并写入output_docusaurus/multi-md/。
导出格式
| 格式 | CLI 参数 | 适用场景 |
|---|---|---|
| 多 MD 文件 | -f multi-md |
RAG 检索、按章节管理 |
| 单 MD 文件 | -f single-md |
直接粘贴到 LLM 上下文 |
| JSONL | -f jsonl |
向量数据库、微调数据集 |
multi-md 输出示例:
output/multi-md/
├── guide/
│ ├── getting-started.md
│ └── configuration.md
└── reference/
└── api.md
JSONL 记录示例:
{"source": "https://...", "title": "Getting Started", "breadcrumb": ["Guide", "Getting Started"], "content": "# Getting Started\n...", "site": "VitePress", "site_type": "vitepress"}
完整 CLI 参数
docsforai crawl [OPTIONS] URL
Arguments:
URL 文档站点 URL
Options:
-o, --output PATH 输出目录 [default: ./output]
-f, --format FORMAT 导出格式,可重复使用 (multi-md|single-md|jsonl)
-t, --type TYPE 强制站点类型 (vitepress|docsify|mintlify|generic)
--concurrency INT 最大并发请求数 [default: 5]
--delay FLOAT 请求间隔秒数 [default: 0.1]
--timeout FLOAT HTTP 超时秒数 [default: 30.0]
--max-pages INT 最大爬取页数,仅 generic 模式 [default: 200]
-V, --version 显示版本
项目结构
src/docsforai/
├── cli.py # Typer CLI 入口
├── detector.py # 自动识别站点类型
├── converter.py # HTML → Markdown(保留代码块语言标注)
├── models.py # 数据模型:DocSite / DocPage / NavItem
├── crawlers/
│ ├── base.py # 抽象基类(限速、并发控制)
│ ├── vitepress.py # VitePress 专用爬虫
│ ├── docsify.py # Docsify 专用爬虫(直取 .md 源文件)
│ ├── mintlify.py # Mintlify 专用爬虫(llms-full.txt / llms.txt)
│ └── generic.py # 通用 BFS 兜底爬虫
└── exporters/
├── multi_md.py # 多 MD 文件导出
├── single_md.py # 单 MD 文件导出
└── llm.py # JSONL 导出
运行时依赖
| 包 | 用途 |
|---|---|
httpx |
异步 HTTP 客户端 |
beautifulsoup4 + lxml |
HTML 解析 |
markdownify |
HTML → Markdown 转换 |
typer + rich |
CLI 界面 |
开发安装
git clone https://github.com/dx2331lxz/DocsForAI.git
cd DocsForAI
pip install -e ".[dev]"
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
docsforai-0.2.0.tar.gz
(23.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
docsforai-0.2.0-py3-none-any.whl
(33.3 kB
view details)
File details
Details for the file docsforai-0.2.0.tar.gz.
File metadata
- Download URL: docsforai-0.2.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50669b1ad4d699c26c1182b25ab3eccf10a68a2422852cca2eca599eabdd8a34
|
|
| MD5 |
26d4f8b97e689be6b551356a3432178b
|
|
| BLAKE2b-256 |
c1cb020999e099910b6382f348f4f16ae18ab88cec172ca1f6c992d11435c999
|
File details
Details for the file docsforai-0.2.0-py3-none-any.whl.
File metadata
- Download URL: docsforai-0.2.0-py3-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ed26ef121e89a461015eec947166482e8c4eb04ccd2bc8f975fd7e194660402
|
|
| MD5 |
50422dba031e2696a162ce048b8773cf
|
|
| BLAKE2b-256 |
4b65a320b549033c75bda6afc2509e801281d427dee2f067f40af203ed602258
|