Install
openclaw skills install @qq435912743/web-fetch网页数据采集技能。静态页用 requests+BeautifulSoup,JS 渲染页用 Playwright,自动尊重 robots.txt、轮换 User-Agent、限速,输出结构化 JSON/CSV。覆盖垂直领域采集、反爬应对、数据清洗与质量校验。适用于市场调研、竞品监控、公开数据聚合。
openclaw skills install @qq435912743/web-fetch从"一次性 fragilescript"到"可恢复、结构化、守规矩"的采集流程。
| 场景 | 工具 | 说明 |
|---|---|---|
| 静态 HTML(新闻/博客/Wiki/政府数据) | requests + BeautifulSoup + lxml | 快、轻、免费 |
| JS 渲染 SPA(React/Vue/Angular) | Playwright(真实 Chromium) | 等 JS 渲染后取 DOM |
| 大规模/分布式 | Scrapy | 内置并发、节流、管道 |
| 不想管浏览器基础设施 | 托管抓取 API | 远端跑 Chromium,返回渲染 HTML |
黄金法则:先查目标站有没有公开 API——更快、更稳、更合规。
from urllib.robotparser import RobotFileParser
rp = RobotFileParser(); rp.set_url("https://example.com/robots.txt"); rp.read()
if not rp.can_fetch("*", url): # 尊重 robots.txt
... # 换源或放弃
# 静态页:提取所有 <a> 链接与标题
python scripts/scrape.py "https://news.ycombinator.com/" --select "span.titleline>a" --attr href --text
# 自定义字段(CSS 选择器 -> 字段名)
python scripts/scrape.py "https://example.com/products" \
--field "title:.product-card h2" --field "price:.price"
# JS 渲染页(Playwright)
python scripts/scrape.py "https://spa.example.com/" --js --wait ".product-card"
# 输出到文件
python scripts/scrape.py "URL" --field "x:y" --out result.json
# 跳过 robots 检查(仅当你已获授权)
python scripts/scrape.py "URL" --no-robots
本技能用 scripts/learner.py 记录每次采集成败与高频失败模式(封禁类型、选择器失效等):
python scripts/learner.py record <技能目录> --capability 反爬绕过 --note "目标站需Playwright+stealth"
python scripts/learner.py record <技能目录> --capability 选择器解析 --fail --error 选择器失效 --note "改版后 .price 变 .cost"
python scripts/learner.py insight <技能目录>
python scripts/learner.py reflect <技能目录>
记忆落盘 learned_patterns.json,跨会话积累,使本技能越用越抗变、越用越准。