介绍
# Playwright Scraper Skill
一个基于 Playwright 的网页抓取 OpenClaw 技能,具备反爬虫保护功能。请根据目标网站的反爬虫等级选择最佳方案。
---
## 🎯 使用场景矩阵
| 目标网站 | 反爬虫等级 | 推荐方法 | 脚本 | |---------------|----------------|-------------------|--------| | **普通网站** | 低 | web_fetch 工具 | N/A (内置) | | **动态网站** | 中 | Playwright Simple | `scripts/playwright-simple.js` | | **Cloudflare 保护** | 高 | **Playwright Stealth** ⭐ | `scripts/playwright-stealth.js` | | **YouTube** | 特殊 | deep-scraper | 需单独安装 | | **Reddit** | 特殊 | reddit-scraper | 需单独安装 |
---
## 📦 安装
```bash cd playwright-scraper-skill npm install npx playwright install chromium ```
---
## 🚀 快速开始
### 1️⃣ 简单网站(无反爬虫)
使用 OpenClaw 内置的 `web_fetch` 工具:
```bash # Invoke directly in OpenClaw Hey, fetch me the content from https://example.com ```
---
### 2️⃣ 动态网站(需要 JavaScript)
使用 **Playwright Simple**:
```bash node scripts/playwright-simple.js "https://example.com" ```
**示例输出:** ```json { "url": "https://example.com", "title": "Example Domain", "content": "...", "elapsedSeconds": "3.45" } ```
---
### 3️⃣ 反爬虫保护网站(Cloudflare 等)
使用 **Playwright Stealth**:
```bash node scripts/playwright-stealth.js "https://m.discuss.com.hk/#hot" ```
**特性:** - 隐藏自动化特征(`navigator.webdriver = false`) - 真实的 User-Agent(iPhone, Android) - 随机延迟以模拟人类行为 - 支持截图和 HTML 保存
---
### 4️⃣ YouTube 视频字幕
使用 **deep-scraper**(需单独安装):
```bash # Install deep-scraper skill npx clawhub install deep-scraper
# Use it cd skills/deep-scraper node assets/youtube_handler.js "https://www.youtube.com/watch?v=VIDEO_ID" ```
---
## 📖 脚本说明
### `scripts/playwright-simple.js` - **使用场景:** 普通动态网站 - **速度:** 快(3-5 秒) - **反爬虫:** 无 - **输出:** JSON(标题、内容、URL)
### `scripts/playwright-stealth.js` ⭐ - **使用场景:** 具有 Cloudflare 或反爬虫保护的网站 - **速度:** 中等(5-20 秒) - **反爬虫:** 中-高(隐藏自动化特征,真实 UA) - **输出:** JSON + 截图 + HTML 文件 - **验证:** 在 Discuss.com.hk 上成功率 100%
---
## 🎓 最佳实践
### 1. 优先尝试 web_fetch 如果网站没有动态加载,请使用 OpenClaw 的 `web_fetch` 工具——这是最快的方式。
### 2. 需要 JavaScript?使用 Playwright Simple 如果需要等待 JavaScript 渲染,请使用 `playwright-simple.js`。
### 3. 被拦截了?使用 Stealth 如果遇到 403 或 Cloudflare 质询,请使用 `playwright-stealth.js`。
### 4. 特殊网站需要专用技能 - YouTube → deep-scraper - Reddit → reddit-scraper - Twitter → bird skill
---
## 🔧 自定义配置
所有脚本均支持环境变量:
```bash # Set screenshot path SCREENSHOT_PATH=/path/to/screenshot.png node scripts/playwright-stealth.js URL
# Set wait time (milliseconds) WAIT_TIME=10000 node scripts/playwright-simple.js URL
# Enable headful mode (show browser) HEADLESS=false node scripts/playwright-stealth.js URL
# Save HTML SAVE_HTML=true node scripts/playwright-stealth.js URL
# Custom User-Agent USER_AGENT="Mozilla/5.0 ..." node scripts/playwright-stealth.js URL ```
---
## 📊 性能对比
| 方法 | 速度 | 反爬虫能力 | 成功率 (Discuss.com.hk) | |--------|-------|----------|-------------------------------| | web_fetch | ⚡ 最快 | ❌ 无 | 0% | | Playwright Simple | 🚀 快 | ⚠️ 低 | 20% | | **Playwright Stealth** | ⏱️ 中等 | ✅ 中等 | **100%** ✅ | | Puppeteer Stealth | ⏱️ 中等 | ✅ 中-高 | ~80% | | Crawlee (deep-scraper) | 🐢 慢 | ❌ 被检测 | 0% | | Chaser (Rust) | ⏱️ 中等 | ❌ 被检测 | 0% |
---
## 🛡️ 反爬虫技术总结
我们测试中得到的经验:
### ✅ 有效的反爬虫措施 1. **隐藏 `navigator.webdriver`** — 必须项 2. **真实的 User-Agent** — 使用真实设备(iPhone, Android) 3. **模拟人类行为** — 随机延迟、滚动 4. **避免框架特征** — Crawlee、Selenium 容易被检测 5. **使用 `addInitScript` (Playwright)** — 在页面加载前注入
### ❌ 无效的反爬虫措施 1. **仅更改 User-Agent** — 不够 2. **使用高级框架 (Crawlee)** — 更容易被检测 3. **Docker 隔离** — 对 Cloudflare 无帮助
---
## 🔍 故障排查
### 问题:403 Forbidden **解决方案:** 使用 `playwright-stealth.js`
### 问题:Cloudflare 质询页面 **解决方案:** 1. 增加等待时间(10-15 秒) 2. 尝试 `headless: false`(有头模式有时成功率更高) 3. 考虑使用代理 IP
### 问题:空白页面 **解决方案:** 1. 增加 `waitForTimeout` 2. 使用 `waitUntil: 'networkidle'` 或 `'domcontentloaded'` 3. 检查是否需要登录
---
## 📝 记录与经验
### 2026-02-07 Discuss.com.hk 测试结论 - ✅ **纯 Playwright + Stealth** 成功(5秒,200 OK) - ❌ Crawlee (deep-scraper) 失败(403) - ❌ Chaser (Rust) 失败(Cloudflare) - ❌ Puppeteer 标准版失败(403)
**最佳方案:** 纯 Playwright + 反爬虫技术(独立于框架)
---
## 🚧 未来改进
- [ ] 添加代理 IP 轮换 - [ ] 实现 Cookie 管理(维持登录状态) - [ ] 添加验证码处理(2captcha / Anti-Captcha) - [ ] 批量抓取(并行 URL) - [ ] 集成 OpenClaw 的 `browser` 工具
---
## 📚 参考资料
- [Playwright 官方文档](https://playwright.dev/) - [puppeteer-extra-plugin-stealth](https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-stealth) - [deep-scraper skill](https://clawhub.com/opsun/deep-scraper)