Skip to content

dean2021/web-to-markdown

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Web to Markdown

将网页转换为 Markdown 文件的工具,支持图片下载和无头浏览器渲染。


Claude Code 技能使用(推荐)

如果你是 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 5

Python API

from 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

Markdown 示例

---
title: "文章标题"
source: "https://example.com/article"
generated: "2024-01-01T12:00:00.000000"
---

# 文章标题

## 一级标题

正文内容...

![描述](images/img_hash.png)

图片支持

  • 自动下载格式: PNG, JPG, GIF, WebP, SVG, ICO
  • 文件名: 基于 URL 的 MD5 哈希值,确保唯一性
  • 相对路径: Markdown 中使用 images/filename.ext 引用
  • Alt 文本: 保留原始图片的 alt 属性

反检测措施

  1. 随机 User-Agent: 从真实浏览器 UA 池中随机选择
  2. Stealth 脚本: 移除 navigator.webdriver 标志
  3. 真实浏览器头: Accept, Accept-Language, Accept-Encoding 等
  4. 视口设置: 1920x1080 分辨率,模拟真实浏览器

依赖

  • playwright>=1.40.0 - 无头浏览器
  • beautifulsoup4>=4.12.0 - HTML 解析
  • readability-lxml>=0.8.1 - 正文提取

许可证

MIT License

About

将网页转换为 Markdown 文件的工具,支持图片下载和无头浏览器渲染。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages