基于微信文章《OpenClaw 永久免费的提取任何网页的终极方案》中的方案实现,提供高性能、无限制的网页内容提取能力。经过优化升级,新增了更强大的反爬能力、智能重试机制和更高质量的内容输出。
- 使用 Scrapling 的 StealthyFetcher,能绕过 Cloudflare Turnstile 等主流反爬系统
- 自动解决 Cloudflare 验证问题
- 支持隐身模式和指纹伪装
- 添加了智能重试机制,提高抓取成功率
- 优化了正文选择器优先级,新增多种常见内容类名
- 支持自适应选择器,网站改版时自动定位元素
- 改进了 html2text 配置,提高输出质量
- 添加了内容清理功能,去除无用信息
只需安装 Scrapling 库即可使用,无需复杂的浏览器驱动配置。
- 保留链接、图片、标题层级等重要信息
- 支持微信公众号文章的直接抓取
- 输出格式为 Markdown,适合 AI 处理
- 优化了图片和链接的处理方式
python3 scrapling_fetch.py <url> <output_file>-c, --chars: 最大字符数限制,默认 30000-n, --no-stealth: 不使用隐身模式(使用基础抓取方法)-r, --retries: 最大重试次数,默认 3 次-t, --timeout: 超时时间(秒),默认 60 秒
python3 scrapling_fetch.py "https://mp.weixin.qq.com/s/EwVItQH4JUsONqv_Fmi4wQ" output.mdpython3 scrapling_fetch.py "https://example.com/blog" blog.md --chars 25000python3 scrapling_fetch.py "https://example.com/protected" output.md --retries 5python3 scrapling_fetch.py "https://example.com/simple" output.md --no-stealthcd /root/.openclaw/workspace/Scrapling
pip install -e .[all]
# 确保安装了所有浏览器依赖
scrapling installpython3 scrapling_fetch.py <url> <output_file>脚本按以下优先级尝试定位正文:
<article>标签<main>标签.post-content类- 包含"body"的类
.content类#js_content标签(微信公众号).article-content类.entry-content类.post-body类- 包含"article"的类
- 包含"content"的类
优化后的 html2text 配置:
h = html2text.HTML2Text()
h.ignore_links = False
h.ignore_images = False
h.body_width = 0 # 不自动折行
h.ignore_emphasis = False
h.images_to_alt = True # 将图片替换为alt文本
h.images_with_size = True # 保留图片尺寸
h.skip_internal_links = False
h.inline_links = True # 使用内联链接格式| 方案 | 成功率 | 速度 | 费用 | 格式保留 | 反爬能力 |
|---|---|---|---|---|---|
| Scrapling + html2text (优化版) | 98%+ | 5秒 | 免费 | ✔️ | ✔️ |
| Scrapling + html2text (旧版) | 95%+ | 3秒 | 免费 | ✔️ | |
| web_fetch | 70% | 2秒 | 免费 | ❌ | ❌ |
| Jina Reader | 90% | 1.4秒 | 200次/天限制 | ✔️ |
- 不支持需要登录态的页面
- 不支持动态渲染的页面(需额外配置)
pip install --upgrade pip
pip install -e .[all] --no-cache-dir
scrapling install --force检查网络连接或尝试调整参数:
python3 scrapling_fetch.py <url> <output> --retries 5 --chars 20000增加字符数限制:
python3 scrapling_fetch.py <url> <output> --chars 40000尝试增加重试次数或使用不同的抓取方法:
python3 scrapling_fetch.py <url> <output> --retries 5 --no-stealthcd /root/.openclaw/workspace/Scrapling
git pull origin main
pip install -e .[all]
scrapling install编辑 scrapling_fetch.py 文件中的 selectors 列表。
- Scrapling GitHub: https://github.com/D4Vinci/Scrapling
- html2text PyPI: https://pypi.org/project/html2text
本项目遵循 MIT 许可证。