Scrapling 深度调研:Python 全栈自适应网页抓取框架
调研时间:2026-08-12
数据来源:GitHub 官方仓库(D4Vinci/Scrapling,main 分支)、官方文档 scrapling.readthedocs.io、PyPI 元数据
调研方式:GitHub API + 官方文档抓取 + PyPI 元数据
目录
- 项目概述
- 核心功能全景
- 架构原理
- 安装与快速上手
- 抓取器详解
- 解析引擎与元素查询
- 反检测机制
- 爬虫框架 Spiders
- 与 Crawl4AI / Playwright 的差异对比
- 优缺点与适用场景
- 参考资源
1. 项目概述
Scrapling是由 D4Vinci 开发的 Python 网页抓取框架,从早期的 stealthy 反检测库演进为一个全栈自适应爬虫框架——覆盖静态抓取、动态渲染、反检测、LLM 集成、完整爬虫编排。
| 维度 | 数据 |
|---|---|
| 项目名 | Scrapling 🕷️ |
| 定位 | Adaptive Web Scraping & Crawling Framework(自适应网页抓取框架) |
| GitHub Stars | 73,555(截至 2026-08-12) |
| Forks | 7,344 |
| 主语言 | Python |
| 许可证 | BSD-3-Clause |
| PyPI 版本 | 0.4.14(>= Python 3.10) |
| 最近发布 | 2026-08-10 |
| PyPI 最新版 | 0.4.14(2026-08-10) |
| 前身 | stealthy(反检测核心库) |
发展脉络:
- 早期
stealthy:聚焦 TLS 指纹伪装与反检测; - 2024-10 发布 Scrapling:在 stealthy 基础上整合解析引擎、LLM 提取;
- 0.3.x:完善 Fetcher 家族(静态/动态/反检测三件套);
- 0.4.x:引入Spiders 爬虫框架(Scrapy 风格)、MCP Server、Agent Skill、Streaming 模式——从"抓取库"升级为"完整爬虫框架"。
一句话定位:抓静态快、抓动态稳、反检测强,还内置 LLM 提取与完整爬虫编排——Scrapling 是当前 Python 生态中"单库能力最全"的爬虫框架。
2. 核心功能全景
2.1 多形态抓取器(Fetcher 家族)
| 抓取器 | 类型 | 适用场景 |
|---|---|---|
Fetcher | HTTP | 静态页面、API;TLS 指纹伪装;HTTP/3 |
DynamicFetcher | 浏览器 | 动态渲染页面(Playwright Chromium / 系统 Chrome) |
StealthyFetcher | 浏览器+反检测 | 强反爬站点(Cloudflare Turnstile/Interstitial) |
AdaptiveFetcher | 自动选择 | 自动判断用哪个抓取器 |
AsyncFetcher等 | 异步 | 高并发 |
2.2 会话管理(Session)
多种会话类统一管理 Cookie 与状态:
FetcherSession:HTTP 会话;StealthySession:反检测会话;DynamicSession:浏览器会话;- 支持代理轮换(ProxyRotator)、广告拦截(内置 ~3,500 广告域名)、DNS 防泄漏(DoH)。
2.3 反检测能力(核心卖点)
- TLS 指纹伪装:HTTP 层模仿真实浏览器的 TLS 指纹;
- 浏览器反检测:
StealthyFetcher注入反检测脚本,可绕过 Cloudflare Turnstile/Interstitial; - 指纹轮换:多指纹会话,降低风控关联风险;
- 远程浏览器/CDP:支持连接已运行的浏览器(
cdp_url),可接托管浏览器服务。
2.4 AI / LLM 集成
- MCP Server:内置 MCP(Model Context Protocol)服务器,供 AI 客户端(Claude/Cursor)驱动抓取与提取,减少 token 消耗;
- Agent Skill:可安装的 Agent 技能包,教会编码 Agent 正确使用 Scrapling API;
- LLM 提取管线:抓取 → 分块 → LLM 结构化提取。
2.5 完整爬虫框架(Spiders,0.4 新增)
Scrapy 风格 API:
start_urls+ 异步parse()回调;- 并发控制、域名级限速(AutoThrottle 自动调节延迟);
- 暂停/恢复(Ctrl+C 优雅停机,重启续爬);
- Streaming 模式:
async for item in spider.stream()实时输出; - Robots.txt 合规:可选
robots_txt_obey(尊重 Disallow/Crawl-delay); - 开发模式:首次请求缓存到磁盘,后续重放,迭代 parse 逻辑不重复请求;
- 内建模板:
CrawlSpider(规则爬取)、SitemapSpider、XMLFeedSpider、CSVFeedSpider、ShopifySpider(拉取 Shopify 商店全部商品); LinkExtractor:链接提取原语;- 内置导出:JSON/JSONL/CSV/XML。
2.6 元素追踪与智能选择
- Smart Element Tracking:网站改版后自动重定位元素(相似度算法);
- Find Similar Elements:自动查找相似元素;
- 多种选择策略:CSS/XPath/文本搜索/正则/过滤。
3. 架构原理
3.1 分层设计
┌──────────────────────────────────────────────────────┐ │ 用户代码 / CLI / MCP Server │ ├──────────────────────────────────────────────────────┤ │ Spiders 爬虫框架 │ │ (start_urls → parse 回调 → 并发调度 → 输出导出) │ ├──────────────────────────────────────────────────────┤ │ Fetcher 抓取器家族(核心) │ │ Fetcher(HTTP) DynamicFetcher(Playwright) │ │ StealthyFetcher(反检测) AdaptiveFetcher(自动选择) │ │ + Session/Proxy/Ad-block/DNS-leak 管理 │ ├──────────────────────────────────────────────────────┤ │ Parser 解析引擎(BS4/Selector/LLM) │ ├──────────────────────────────────────────────────────┤ │ stealthy 反检测内核(TLS 指纹 + JS 注入) │ └──────────────────────────────────────────────────────┘3.2 Fetch → Parse 数据流
1. Fetch: 选择抓取器(HTTP 或浏览器) 2. 输入: URL / HTML 字符串 / 文件 3. Parse: HTML → ScraplingPage 对象(惰性选择器) 4. Query: page.css() / page.xpath() / find_by_text() ... 5. Extract: get() / getall() / to_dict() / table_to_dict() 6. LLM: 可选,将内容交给 LLM 结构化提取3.3 反检测内核(stealthy)
HTTP 层(Fetcher):
- 指纹指纹伪装:模仿 Chrome/Safari 等的 TLS 指纹;
- HTTP/3 (QUIC) 支持;
- 请求头伪装。
浏览器层(StealthyFetcher):
- 启动 Playwright Chromium + 注入反检测 JS(隐藏 webdriver 特征、修改 navigator 属性等);
- 支持
stealth.min.js风格脚本注入; - 指纹轮换与并行会话。
3.4 底层浏览器依赖
DynamicFetcher / StealthyFetcher 底层依赖Playwright(Chromium)或系统 Chrome。这意味着 Scrapling 的"动态抓取"能力建立在 Playwright 之上,但封装了会话、代理、反检测等高级功能。
4. 安装与快速上手
4.1 安装
# 基础安装(静态抓取)pipinstallscrapling# 完整安装(含浏览器引擎、LLM 支持、爬虫框架)pipinstall"scrapling[all]"# 可选 extra: [browser](浏览器抓取) [llm](LLM 提取) [spider](爬虫框架)首次使用动态抓取需下载浏览器:
playwright install chromium(Scrapling 复用 Playwright 浏览器)。
4.2 最小示例:静态抓取
fromscrapling.fetchersimportFetcher# GET 请求response=Fetcher.get('https://example.com')print(response.status)# 解析:选择器引擎page=response.parsed title=page.css('title').get()h1_text=page.css('h1::text').get()4.3 动态抓取
fromscrapling.fetchersimportDynamicFetcher page=DynamicFetcher.get('https://spa.example.com')# 自动等页面加载完成,返回已渲染的 DOMitems=page.css('.product-card').getall()4.4 反检测抓取(绕过 Cloudflare)
fromscrapling.fetchersimportStealthyFetcher page=StealthyFetcher.get('https://hard-protected.example.com')ifpage.status==200:content=page.css('#content').get()4.5 自适应抓取
fromscrapling.fetchersimportAdaptiveFetcher# 自动选择最合适的抓取方式page=AdaptiveFetcher.fetch('https://example.com')4.6 CLI 交互式 Shell
scrapling shell https://example.com# 进入交互式解析环境,实时测试选择器5. 抓取器详解
5.1 Fetcher(HTTP 静态)
fromscrapling.fetchersimportFetcher# GET / POST / 自定义方法r=Fetcher.get('https://api.example.com/data',params={'page':1})r=Fetcher.post('https://api.example.com/submit',json={'k':'v'})# 自动解析page=r.parsed# ScraplingPage 对象特性:TLS 指纹伪装、HTTP/3、请求头伪装、超时/重试控制。
5.2 DynamicFetcher(动态渲染)
fromscrapling.fetchersimportDynamicFetcher# 支持 Playwright 的 Chromium 或系统 Chromepage=DynamicFetcher.get('https://spa.example.com',browser='chromium',# 或 'chrome'(系统 Chrome)headless=True,network_idle=True,# 等待网络空闲wait_selector='#app-loaded',# 等待元素)5.3 StealthyFetcher(反检测)
fromscrapling.fetchersimportStealthyFetcher page=StealthyFetcher.get('https://protected.example.com',fingerprint='chrome-131',# 指定浏览器指纹use_stealth=True,# 注入反检测)5.4 AdaptiveFetcher(自动选择)
AdaptiveFetcher.fetch()根据目标网站的特征自动决定用静态 HTTP 还是浏览器抓取——一个入口覆盖所有场景。
6. 解析引擎与元素查询
6.1 解析框架
| 解析器 | 说明 |
|---|---|
| BS4(BeautifulSoup) | 默认解析,兼作底层 |
| Selector(parsel/Scrapy Selector) | 高性能 CSS/XPath 查询,支持伪元素::text |
| LLM Parser | 交给 LLM 做语义提取 |
| JsonParser | JSON 响应解析 |
| LightParser | 轻量快速解析(无完整 DOM) |
6.2 元素查询示例
# CSS 选择器(支持 ::text 伪元素)page.css('div.quote').getall()# 多个元素page.css('div.quote .text::text').get()# 文本# XPathpage.xpath('//div[@class="quote"]').getall()# 文本查找page.find_by_text('quote',tag='div')# 正则page.find_by_regex(r'\d+\.\d+')# 链式选择quote=page.css('.quote')[0]author=quote.css('.author::text').get()# 元素关系parent=quote.parent()siblings=quote.next_siblings()6.3 数据提取
page.to_dict()# 转字典page.table_to_dict()# 表格转字典(抓表格神器)element.get()# 单值element.getall()# 多值6.4 智能元素追踪
# 网站改版后自动重定位target=page.css('.old-selector')new_target=target.locate_me()# 相似度算法重新定位7. 反检测机制
7.1 认证流程中需要解决的两类反爬
| 反爬类型 | Scrapling 方案 |
|---|---|
| Cloudflare Turnstile / Interstitial | StealthyFetcher 自动处理(模拟交互 + 指纹伪装) |
| TLS 指纹检测(JA3/JA4) | 伪造浏览器 TLS 指纹 |
| 请求头检测 | 伪造 UA、Accept、Sec-Fetch 等 |
| 浏览器指纹(navigator/webdriver/Canvas) | 反检测脚本注入 |
| IP 频率限制 | 内置代理轮换 ProxyRotator |
7.2 注意边界
Scrapling 声称可绕过 Cloudflare Turnstile,但这是攻防的持续博弈;强烈反爬场景(如登录墙+行为验证)仍需人工或更强方案。合规方面:应遵守目标站点 robots.txt 与 ToS。
8. 爬虫框架 Spiders
8.1 最小 Spider
fromscrapling.spidersimportSpiderclassMySpider(Spider):name='myspider'start_urls=['https://quotes.toscrape.com/']asyncdefparse(self,response):forquoteinresponse.css('div.quote'):yield{'text':quote.css('.text::text').get(),'author':quote.css('.author::text').get(),}# 翻页next_page=response.css('li.next a::attr(href)').get()ifnext_page:yieldself.follow(next_page,callback=self.parse)8.2 运行与导出
# 运行my_spider=MySpider()my_spider.run()# 同步运行result=my_spider.result# 导出result.items.to_json('quotes.json')result.items.to_csv('quotes.csv')8.3 暂停/恢复与流式
# 暂停恢复(Ctrl+C 停机,重启续爬)# 流式输出asyncforiteminmy_spider.stream():print(item)8.4 内建模板
- CrawlSpider:基于规则(allow/deny)的链接跟随爬取;
- SitemapSpider:基于 sitemap.xml / robots.txt 的爬取;
- XMLFeedSpider / CSVFeedSpider:迭代 XML/RSS / CSV 数据源;
- ShopifySpider:从任意 Shopify 商店经 JSON API 拉取全部商品。
9. 与 Crawl4AI / Playwright 的差异对比
| 维度 | Scrapling | Crawl4AI | Playwright |
|---|---|---|---|
| 定位 | 全栈自适应爬虫框架 | LLM 数据管道爬虫 | 浏览器自动化底层 |
| 语言 | Python | Python | 多语言(Python/JS/Java/.NET) |
| 静态抓取 | ✅ Fetcher(TLS 伪装) | ⚠️ 侧重 Markdown | ❌ 需启浏览器 |
| 动态渲染 | ✅ DynamicFetcher | ✅ Playwright 集成 | ✅ 原生 |
| 反检测 | ✅ StealthyFetcher(强) | ⚠️ 有限 | ⚠️ 需第三方 stealth |
| LLM 提取 | ✅ LLM Parser + MCP | ✅ 最强(专为 LLM) | ❌ 无 |
| Markdown 输出 | 基本 | ✅ 最强(LLM 友好) | ❌ 需自行转换 |
| 爬虫编排 | ✅ Spiders(完整框架) | ✅ DeepCrawl | ❌ 需自己写 |
| 浏览器下载 | ✅ 复用 Playwright | ✅ 自带 | ✅ 自带 |
| 学习成本 | 中等 | 低(API 简洁) | 中高 |
| 适用 | 反爬 + 全栈抓取 | 喂 LLM/RAG 的数据抓取 | 精细浏览器自动化 |
分工建议:
- 有强反爬需求 → Scrapling(StealthyFetcher 是最大差异点);
- 主要喂 LLM/RAG→ Crawl4AI(Markdown + 提取管线最优);
- 需要细粒度浏览器控制(点击/拖拽/复杂交互) → Playwright 原生。
10. 优缺点与适用场景
优点
- 能力最全:静态/动态/反检测/LLM/爬虫编排一库搞定;
- 反检测突出:TLS 指纹 + StealthyFetcher,Cloudflare 场景实测有效;
- 解析灵活:CSS/XPath/文本/正则多策略 + 智能元素追踪;
- 爬虫框架完整:0.4 引入的 Spiders 有暂停恢复、限速、robots 合规、导出等生产级特性;
- MCP/AI 集成:面向 AI 时代的原生设计。
缺点
- 版本迭代快(0.4.x 仍 Beta),API 稳定性一般;
- 文档相对稀疏,社区示例少于 Crawl4AI;
- 动态抓取底层仍是 Playwright,绕不开浏览器体积与性能成本;
- 反检测能力是攻防博弈,强风控站点无法保证永久有效。
适用场景
- 需要绕过 Cloudflare 的抓取任务;
- 一个库同时要静态+动态+反检测的敏捷项目;
- 需要完整爬虫编排(多页、限速、恢复、导出)的采集系统;
- 已接入 AI Agent 体系的抓取(MCP Server)。
11. 参考资源
- GitHub:https://github.com/D4Vinci/Scrapling(73.6K stars,BSD-3-Clause)
- 官方文档:https://scrapling.readthedocs.io/
- PyPI:https://pypi.org/project/scrapling/(v0.4.14)
- PyPI 最新版:0.4.14(2026-08-10)