📦 OpenClaw Scrapling — 反爬虫抓取
v1.0.0基于 Playwright 的浏览器级抓取工具,自动绕过 Cloudflare、JS 挑战与指纹检测,支持动态渲染、代理、登录会话、自适应选择器与多格式输出,可应对站点改版。
详细分析 ▾
运行时依赖
版本
Scrapling 技能初始发布,用于高级、弹性网页抓取。 - 支持抓取带反机器人保护、JavaScript 渲染内容及频繁 UI 变更的站点。 - 隐身模式可绕过 Cloudflare、机器人检测并使用浏览器指纹欺骗。 - 通过基于 Playwright 的自动化处理动态内容,自适应选择器在站点改版时仍保持稳健抓取。 - 包含登录站点的会话管理,支持代理、速率限制及自定义请求头。 - 提供多种提取模式(文本、markdown、属性、多字段)及输出格式(JSON、CSV、TXT、MD、HTML)。 - 同时提供 CLI 命令与 Python API,灵活易用。
安装命令
点击复制技能文档
使用 Scrapling来抓取现代网站,包括那些具有反机器人保护、JavaScript渲染内容和自适应元素跟踪的网站。
何时使用此技能
- 用户要求抓取网站或从 URL 提取数据
- 需要绕过 Cloudflare、机器人检测或反爬取措施
- 需要处理 JavaScript 渲染/动态内容(React、Vue 等)
- 网站需要登录或会话管理
- 网站结构频繁变化(自适应选择器)
- 需要以速率限制抓取多个页面
命令
所有命令都使用此技能目录中的 scrape.py 脚本。
基础 HTTP 抓取(快速)
python scrape.py \
--url "https://example.com" \
--selector ".product" \
--output products.json
适用场景: 静态 HTML,无 JavaScript,无机器人保护
隐身模式(绕过反机器人)
python scrape.py \
--url "https://nopecha.com/demo/cloudflare" \
--stealth \
--selector "#content" \
--output data.json
适用场景: Cloudflare 保护、机器人检测、指纹识别
功能特点:
- 自动绕过 Cloudflare Turnstile
- 浏览器指纹欺骗
- 无头浏览器模式
动态/JavaScript 内容
python scrape.py \
--url "https://spa-website.com" \
--dynamic \
--selector ".loaded-content" \
--wait-for ".loaded-content" \
--output data.json
适用场景: React/Vue/Angular 应用、懒加载内容、AJAX
功能特点:
- 完整的 Playwright 浏览器自动化
- 等待元素加载
- 网络空闲检测
自适应选择器(适应网站变化)
# 首次运行 - 保存选择器模式 python scrape.py \ --url "https://example.com" \ --selector ".product-card" \ --adaptive-save \ --output products.json
# 之后,如果网站结构发生变化 python scrape.py \ --url "https://example.com" \ --adaptive \ --output products.json
适用场景: 网站频繁改版、需要健壮的抓取
工作原理:
- 首次运行:保存元素模式/结构
- 后续运行:使用相似性算法重新定位移动的元素
- 自动更新选择器缓存
会话管理(需要登录)
# 登录并保存会话 python scrape.py \ --url "https://example.com/dashboard" \ --stealth \ --login \ --username "user@example.com" \ --password "password123" \ --session-name "my-session" \ --selector ".protected-data" \ --output data.json
# 重用保存的会话(无需登录) python scrape.py \ --url "https://example.com/another-page" \ --stealth \ --session-name "my-session" \ --selector ".more-data" \ --output more_data.json
适用场景: 内容需要认证、多步骤抓取
提取特定数据类型
仅文本:
python scrape.py \
--url "https://example.com" \
--selector ".content" \
--extract text \
--output content.txt
Markdown:
python scrape.py \
--url "https://docs.example.com" \
--selector "article" \
--extract markdown \
--output article.md
属性:
# 提取 href 链接
python scrape.py \
--url "https://example.com" \
--selector "a.product-link" \
--extract attr:href \
--output links.json
多个字段:
python scrape.py \
--url "https://example.com/products" \
--selector ".product" \
--fields "title:.title::text,price:.price::text,link:a::attr(href)" \
--output products.json
高级选项
代理支持:
python scrape.py \
--url "https://example.com" \
--proxy "http://user:pass@proxy.com:8080" \
--selector ".content"
速率限制:
python scrape.py \
--url "https://example.com" \
--selector ".content" \
--delay 2 # 请求之间等待 2 秒
自定义请求头:
python scrape.py \
--url "https://api.example.com" \
--headers '{"Authorization": "Bearer token123"}' \
--selector "body"
截图(用于调试):
python scrape.py \
--url "https://example.com" \
--stealth \
--screenshot debug.png
Python API(用于自定义脚本)
您也可以直接在 Python 脚本中使用 Scrapling:
from scrapling.fetchers import Fetcher, StealthyFetcher, DynamicFetcher# 基础 HTTP 请求 page = Fetcher.get('https://example.com') products = page.css('.product') for product in products: title = product.css('.title::text').get() price = product.css('.price::text').get() print(f"{title}: {price}")
# 隐身模式(绕过反机器人) page = StealthyFetcher.fetch('https://protected-site.com', headless=True) data = page.css('.content').getall()
# 动态内容(完整浏览器) page = DynamicFetcher.fetch('https://spa-app.com', network_idle=True) items = page.css('.loaded-item').getall()
# 会话(登录) from scrapling.fetchers import StealthySession
with StealthySession(headless=True) as session: # 登录 login_page = session.fetch('https://example.com/login') login_page.fill('#username', 'user@example.com') login_page.fill('#password', 'password123') login_page.click('#submit')
# 访问受保护的内容 protected_page = session.fetch('https://example.com/dashboard') data = protected_page.css('.private-data').getall()
输出格式
- JSON(默认):
--output data.json - JSONL(流式):
--output data.jsonl - CSV:
--output data.csv - TXT(纯文本):
--output data.txt - MD(markdown):
--output data.md - HTML(原始):
--output data.html
选择器类型
Scrapling 支持多种选择器格式:
CSS 选择器:
--selector ".product"
--selector "div.container > p.text"
--selector "a[href*='product']"
XPath 选择器:
--selector "//div[@class='product']"
--selector "//a[contains(@href, 'product')]"
伪元素(类似 Scrapy):
--selector ".product::text" # 文本内容
--selector "a::attr(href)" # 属性值
--selector ".price::text::strip" # 去除空白的文本
组合选择器:
--selector ".product .title::text" # 嵌套元素
故障排除
问题:"Element not found"(未找到元素)
- 如果内容是 JavaScript 加载的,请尝试
--dynamic - 使用
--wait-for SELECTOR等待元素出现 - 使用
--screenshot查看可见内容进行调试
问题:"Cloudflare blocking"(Cloudflare 阻止)
- 使用
--stealth模式 - 添加
--solve-cloudflare标志(隐身模式下默认启用) - 尝试
--delay 2减慢请求速度
问题:"Login not working"(登录不工作)
- 使用
--headless false查看浏览器交互 - 检查凭据是否正确
- 网站可能使用 CAPTCHA(需要手动干预)
问题:"Selector broke after website update"(网站更新后选择器失效)
- 使用
--adaptive模式自动重新定位元素 - 重新运行
--adaptive-save更新保存的模式
示例
抓取 Hacker News 首页
python scrape.py \
--url "https://news.ycombinator.com" \
--selector ".athing" \
--fields "title:.titleline>a::text,link:.titleline>a::attr(href)" \
--output hn_stories.json
使用登录抓取受保护网站
python scrape.py \
--url "https://example.com/data" \
--stealth \
--login \
--username "user@example.com" \
--password "secret" \
--session-name "example-session" \
--selector ".data-table tr" \
--output protected_data.json
监控价格变化
# 保存初始选择器模式 python scrape.py \ --url "https://store.com/product/123" \ --selector ".price" \ --adaptive-save \ --output price.txt
# 之后检查价格(即使页面已改版) python scrape.py \ --url "https://store.com/product/123" \ --adaptive \ --output price_new.txt
抓取动态 JavaScript 应用
python scrape.py \
--url "https://react-app.com/data" \
--dynamic \
--wait-for ".loaded-content" \
--selector ".item" \
--fields "name:.name::text,value:.value::text" \
--output app_data.json
注意事项
- 首次运行:Scrapling 会下载浏览器(约 500MB)。这是自动完成的。
- 会话:保存在
sessions/目录中,可跨运行重用 - 自适应缓存:保存在
selector_cache.json中,自动更新 - 速率限制:始终尊重
robots.txt并添加延迟以进行道德抓取 - 法律:仅在您有权限抓取的网站上使用
依赖项
安装技能时自动安装:
- scrapling[all] - 包含所有功能的主库
- pyyaml - 用于配置文件支持
技能结构
scrapling/
├── SKILL.md # 本文件
├── scrape.py # 主 CLI 脚本
├── requirements.txt # Python 依赖
├── sessions/ # 保存的浏览器会话
├── selector_cache.json # 自适应选择器模式
└── examples/ # 示例脚本
├── basic.py
├── stealth.py
├── dynamic.py
└── adaptive.py
高级:自定义 Python 脚本
对于复杂的爬取任务,你可以在此目录中创建自定义 Python 脚本:
# custom_scraper.py from scrapling.fetchers import StealthyFetcher from scrapling.spiders import Spider, Response import jsonclass MySpider(Spider): name = "custom" start_urls = ["https://example.com/page1"]
async def parse(self, response: Response): for item in response.css('.product'): yield { "title": item.css('.title::text').get(), "price": item.css('.price::text').get() }
# Follow pagination next_page = response.css('.next-page::attr(href)').get() if next_page: yield response.follow(next_page)
# Run spider result = MySpider().start()
with open('output.json', 'w') as f: json.dump(result.items, f, indent=2)
运行方式:
python custom_scraper.py
有疑问?
查看 Scrapling 文档:https://scrapling.readthedocs.io