将网页转换为 Markdown 文件的工具,支持图片下载和无头浏览器渲染。
如果你是 Claude Code 用户,可以直接安装为技能来使用:
# 进入 Claude Code 技能目录
cd ~/.claude/skills
# 克隆或复制技能
git clone git@github.com:dean2021/web-to-markdown.git web-to-markdown
# 或者直接复制打包文件
cp /path/to/web-to-markdown.skill ~/.claude/skills/安装后,只需在对话中告诉 Claude:
将 https://example.com/article 保存为 markdown
Claude 会自动调用此技能完成转换。
如果需要自定义或开发,可直接使用源码:
# 进入项目目录
cd web-to-markdown
# 安装依赖
pip install -r scripts/requirements.txt
# 安装 Playwright 浏览器
playwright install chromium# 基本用法
python scripts/scrape.py "https://example.com/article"
# 指定输出目录
python scripts/scrape.py "https://example.com/article" -o ./output
# 调整等待时间(适合 SPA 和懒加载页面)
python scripts/scrape.py "https://example.com/article" -w 5from scripts.scrape import scrape_page
# 保存到当前目录
md_path = scrape_page("https://example.com/article")
# 保存到指定目录
md_path = scrape_page(
url="https://example.com/article",
output_dir="./articles",
wait_time=5
)| 参数 | 说明 | 默认值 |
|---|---|---|
url |
要转换的网页 URL | 必填 |
-o, --output |
输出目录 | 当前目录 |
-w, --wait |
页面加载后等待秒数 | 3 |
output-directory/
├── article-title.md # Markdown 文件(含 frontmatter)
└── images/
├── img_abc123def456.jpg
└── img_987fed654abc.png
---
title: "文章标题"
source: "https://example.com/article"
generated: "2024-01-01T12:00:00.000000"
---
# 文章标题
## 一级标题
正文内容...
- 自动下载格式: PNG, JPG, GIF, WebP, SVG, ICO
- 文件名: 基于 URL 的 MD5 哈希值,确保唯一性
- 相对路径: Markdown 中使用
images/filename.ext引用 - Alt 文本: 保留原始图片的 alt 属性
- 随机 User-Agent: 从真实浏览器 UA 池中随机选择
- Stealth 脚本: 移除
navigator.webdriver标志 - 真实浏览器头: Accept, Accept-Language, Accept-Encoding 等
- 视口设置: 1920x1080 分辨率,模拟真实浏览器
playwright>=1.40.0- 无头浏览器beautifulsoup4>=4.12.0- HTML 解析readability-lxml>=0.8.1- 正文提取
MIT License