



























今天在 GitHub Trending 上看到一个有意思的项目:Scrapling,一句话描述项目核心价值——它是一个「自适应」的 Web 爬虫框架,解析器能在网页结构变化时自动重新定位元素,同时内置了绕过 Cloudflare 等反爬机制的能力,真正把现代爬虫的痛点一站式解决了。
Scrapling 由 Karim Shoair(D4Vinci)开发,是一个覆盖「单请求 → 全站并发爬取」全链路的 Python 爬虫框架。它的核心定位是:
项目目前版本 v0.4.8,测试覆盖率 92%,已有数百名爬虫开发者日常使用,支持 Python 3.10+。
Scrapling 的架构可以划分为三层:
┌─────────────────────────────────────┐
│ Spider Framework │ ← 并发爬取、暂停恢复、流式输出
├─────────────────────────────────────┤
│ Fetchers (多种) │ ← HTTP / Stealthy / Dynamic
├─────────────────────────────────────┤
│ Adaptive Parser Engine │ ← 自适应元素追踪、智能相似度算法
└─────────────────────────────────────┘
1. 自适应解析引擎(Adaptive Parser)
这是 Scrapling 最具创新性的部分。传统爬虫最大的维护成本就是「选择器失效」—— 网页改版后 CSS 选择器不再匹配。Scrapling 的 Parser 在初次定位元素时会记录该元素的上下文特征(标签、属性、文本内容、兄弟节点关系等),当页面结构发生变化时,调用 adaptive=True 参数,解析器会通过智能相似度算法重新定位该元素:
# 首次抓取,自动保存元素特征
products = p.css('.product', auto_save=True)
# 网页改版后,传入 adaptive=True 自动重新定位
products = p.css('.product', adaptive=True)
相似度算法综合考虑以下维度:
2. 多层 Fetcher 设计
Scrapling 提供三种 Fetcher,分别适用不同场景:
| Fetcher | 底层技术 | 适用场景 | 反爬能力 |
|---|---|---|---|
Fetcher | curl_cffi (HTTP/3 支持) | 静态页面、高并发 | 中(TLS 指纹伪装) |
StealthyFetcher | Playwright + 指纹欺骗 | 有 Cloudflare 的页面 | 强(完整浏览器环境) |
DynamicFetcher | Playwright Chromium | 重度 JS 渲染页面 | 中(需配合 Stealthy) |
Fetcher 使用 curl_cffi 库,可完整 impersonate Chrome/Firefox 的 TLS 指纹,绕过基于 JA3 指纹的封禁:
from scrapling.fetchers import Fetcher
# 伪装成 Chrome 的最新版本
page = Fetcher.get('https://example.com', impersonate='chrome')
StealthyFetcher 则在真实浏览器环境中执行,并注入指纹欺骗脚本(基于 apify-fingerprint-datapoints),可绕过 Cloudflare Turnstile 人机验证:
from scrapling.fetchers import StealthyFetcher
page = StealthyFetcher.fetch(
'https://nopecha.com/demo/cloudflare',
headless=True,
solve_cloudflare=True, # 自动绕过 Cloudflare
network_idle=True # 等待网络空闲(页面完全加载)
)
3. Spider 并发框架
Spider 的设计灵感来自 Scrapy,但做了大量现代化改进:
concurrent_requests 参数控制全局并发数,同时支持 per-domain 限流async for item in spider.stream() 实时获取爬取结果,适合长时间运行的任务from scrapling.spiders import Spider, Request, Response
from scrapling.fetchers import FetcherSession, AsyncStealthySession
class MultiSessionSpider(Spider):
name = "multi"
start_urls = ["https://example.com/"]
concurrent_requests = 10
def configure_sessions(self, manager):
# 注册两个 Session:「fast」用于普通页面,「stealth」用于反爬严格的页面
manager.add("fast", FetcherSession(impersonate="chrome"))
manager.add("stealth", AsyncStealthySession(headless=True), lazy=True)
async def parse(self, response: Response):
for link in response.css('a::attr(href)').getall():
if "protected" in link:
yield Request(link, sid="stealth") # 走浏览器
else:
yield Request(link, sid="fast") # 走 HTTP
4. MCP Server(AI 集成)
Scrapling 内置了 MCP Server(scrapling[ai]),可与 Claude/Cursor 等 AI 工具对接。MCP Server 会先利用 Scrapling 的解析能力精准提取目标内容,再将结构化数据交给 AI 处理,大幅减少 token 消耗。
Scrapling 的 Parser 基于 lxml 构建,性能与原生 lxml 几乎持平:
| 库 | 5000 元素文本提取 (ms) | 相对 Scrapling |
|---|---|---|
| Scrapling | 2.02 | 1.0x |
| Parsel/Scrapy | 2.04 | 1.01x |
| 原生 lxml | 2.54 | 1.26x |
| PyQuery | 24.17 | ~12x |
| BeautifulSoup (lxml) | 1584.31 | ~784x |
在「按文本内容查找相似元素」的 Benchmark 中,Scrapling 比 AutoScraper 快 5.2 倍。
# 基础安装(仅 Parser,无 Fetcher 依赖)
pip install scrapling
# 完整安装(含 Fetcher、CLI、浏览器支持)
pip install "scrapling[all]"
# 安装浏览器依赖(使用 fetchers 时必须)
scrapling install
scrapling install 会自动下载 Chromium 浏览器及系统依赖,支持 --force 参数强制重装。
场景 1:快速抓取静态页面
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote .text::text').getall()
print(quotes)
场景 2:绕过 Cloudflare
from scrapling.fetchers import StealthyFetcher
page = StealthyFetcher.fetch(
'https://nopecha.com/demo/cloudflare',
headless=True,
solve_cloudflare=True
)
print(page.css('#padded_content a::text').getall())
场景 3:完整 Spider 爬取
from scrapling.spiders import Spider, Response
class QuotesSpider(Spider):
name = "quotes"
start_urls = ["https://quotes.toscrape.com/"]
concurrent_requests = 10
async def parse(self, response: Response):
for quote in response.css('.quote'):
yield {
"text": quote.css('.text::text').get(),
"author": quote.css('.author::text').get(),
}
next_page = response.css('.next a::attr(href)').get()
if next_page:
yield response.follow(next_page)
result = QuotesSpider().start()
result.items.to_json("quotes.json")
Scrapling 提供了强大的 CLI,无需写 Python 代码即可完成抓取:
# 启动交互式 Web 爬取 Shell(基于 IPython)
scrapling shell
# 直接提取网页内容为 Markdown/HTML/TXT
scrapling extract get 'https://example.com' output.md
scrapling extract stealthy-fetch 'https://example.com' output.html \
--solve-cloudflare --no-headless
from scrapling.spiders import Spider, Response
from scrapling.fetchers import FetcherSession
class PriceMonitorSpider(Spider):
name = "price_monitor"
start_urls = ["https://shop.example.com/products"]
concurrent_requests = 5
def configure_sessions(self, manager):
# 使用 Chrome TLS 指纹伪装
manager.add("http", FetcherSession(impersonate="chrome"))
async def parse(self, response: Response):
for product in response.css('.product-item'):
yield {
"name": product.css('.name::text').get(),
"price": product.css('.price::text').get(),
"url": response.urljoin(product.css('a::attr(href)').get()),
}
from scrapling.fetchers import FetcherSession
# 内置 ProxyRotator,支持循环/自定义策略
session = FetcherSession(
impersonate="chrome",
proxies=["http://proxy1:8080", "http://proxy2:8080"],
proxy_rotation="cycle" # 循环轮换
)
page = session.get('https://example.com')
FROM ghcr.io/d4vinci/scrapling:latest
# 已预装所有依赖和浏览器,可直接运行
scrapling install 报错问题:执行 scrapling install 时 Playwright 浏览器下载失败。
解决:检查网络连接,或手动指定 Playwright 镜像源:
PLAYWRIGHT_BROWSERS_PATH=/tmp/playwright npx playwright install chromium
也可使用预构建的 Docker 镜像,跳过本地环境配置。
StealthyFetcher 无法绕过 Cloudflare问题:设置了 solve_cloudflare=True 但仍然遇到 Cloudflare 验证页面。
解决:
headless=False 观察实际发生了什么wait_for 参数等待特定元素出现network_idle=True 让页面完全加载问题:高并发请求后 IP 被封。 解决:
concurrent_requests 和 download_delayFetcher(impersonate='chrome') 伪装 TLS 指纹robots_txt_obey=True 遵守目标站的爬取规则问题:网页改版后 adaptive=True 定位到了错误元素。
解决:
auto_save=True 保存元素特征css() 选择时尽量使用具有唯一标识的元素问题:安装时报 requires-python >=3.10。
解决:升级 Python 版本,或使用 pyenv/conda 管理多版本。Scrapling 大量使用了 Python 3.10+ 的类型注解和异步语法,无法向后兼容。
Scrapling 是一个设计非常「现代」的爬虫框架——它既保留了 Scrapy 的并发框架设计,又通过自适应解析器解决了爬虫维护成本最高的「选择器失效」问题,更通过 StealthyFetcher 把 Cloudflare 绕过变成了开箱即用的功能。
对于需要快速上手的开发者,它的单请求 API(Fetcher.get())几行代码就能出结果;对于需要大规模爬取的场景,它的 Spider 框架提供了暂停恢复、流式输出、多 Session 路由等企业级特性。
如果你正在维护一套频繁因网页改版而崩溃的爬虫代码,或者受困于 Cloudflare 等反爬机制,Scrapling 值得一试。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。