Scrapling:极简高效的 Python 智能爬虫框架

1 阅读5分钟

传统 Python 爬虫开发需完成依赖安装、编码处理、Cookie 配置、验证码绕过、分页逻辑编写、解析优化等繁琐流程,开发耗时长达两天;且目标网站改版后,CSS 选择器失效需重新开发,维护成本极高。

Scrapling(GitHub Star 量 52k+,作者 D4Vinci)专为解决上述痛点设计,可将爬虫开发简化为几行代码。其核心优势包含三大特性:自适应元素追踪(网站改版后自动重定位元素)、原生反反爬能力(零配置绕过 Cloudflare Turnstile)、类 Scrapy Spider 框架(支持并发爬取、断点续爬、代理轮换)。本文将通过实战代码演示核心功能落地。

环境要求

Python 3.10 及以上版本

安装方式

bash

运行

# 基础安装(仅包含HTML解析器)
pip install scrapling

# 完整安装(含请求器、浏览器驱动、反指纹依赖)
pip install "scrapling[fetchers]"
scrapling install

<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">scrapling install</font> 会自动下载 Chromium 浏览器、Camoufox 反指纹套件及系统依赖,国内网络环境建议使用代理,安装耗时约 10-20 分钟。

bash

运行

# 全功能安装(含MCP Server、交互式Shell)
pip install "scrapling[all]"
scrapling install

Docker 用户可直接拉取官方镜像:

bash

运行

docker pull ghcr.io/d4vinci/scrapling:latest

核心功能实战

一、一体化请求与解析

Scrapling 整合请求与解析流程,返回对象直接支持选择器操作,兼容 CSS、XPath、BeautifulSoup 三种语法且可无缝混用,无需类型转换。

python

运行

from scrapling.fetchers import Fetcher

# 单接口完成网络请求+HTML解析
page = Fetcher.get('https://quotes.toscrape.com/')

# CSS选择器(兼容Scrapy/Parsel语法)
quotes = page.css('.quote .text::text').getall()

# XPath语法
quotes = page.xpath('//span[@class="text"]/text()').getall()

# BeautifulSoup风格语法
quotes = page.find_all('div', class_='quote')

二、智能会话与 Cookie 管理

内置同步 / 异步会话管理器,自动维护 Cookie 生命周期,支持浏览器 TLS 指纹模拟,有效对抗 JA3/JA4 指纹检测。

python

运行

from scrapling.fetchers import FetcherSession

# 同步会话:自动携带Cookie,模拟Chrome指纹
with FetcherSession(impersonate='chrome') as session:
    # 获取登录令牌
    page1 = session.get('https://quotes.toscrape.com/login')
    # 自动提交登录表单
    session.post(
        'https://quotes.toscrape.com/login',
        data={'csrf_token': page1.css('input[name="csrf_token"]::attr(value)').get(),
              'username': 'test', 'password': 'test'}
    )
    # 登录后页面自动携带Cookie
    page2 = session.get('https://quotes.toscrape.com/')

三、原生绕过 Cloudflare 反爬

基于 Camoufox 反指纹引擎,<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">StealthyFetcher</font>可自动处理 Cloudflare Turnstile 验证,无需编写验证码识别逻辑。

python

运行

from scrapling.fetchers import StealthyFetcher, StealthySession

# 单请求绕过验证
page = StealthyFetcher.fetch('https://nopecha.com/demo/cloudflare', headless=True, solve_cloudflare=True)

# 会话模式:复用浏览器上下文,保持登录状态
with StealthySession(headless=True, solve_cloudflare=True) as session:
    page1 = session.fetch('https://example.com/protected-page-1')

说明:该组件对 Cloudflare Turnstile 通过率优异,仅支持 Cloudflare 验证,对 DataDome、Akamai 等企业级反爬系统需配合第三方服务。

四、JavaScript 动态页面渲染

基于 Playwright 的<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">DynamicFetcher</font>可等待 JS 渲染完成,支持资源拦截、广告屏蔽,适配纯前端渲染页面。

python

运行

from scrapling.fetchers import DynamicFetcher, DynamicSession

# 渲染动态页面
page = DynamicFetcher.fetch('https://quotes.toscrape.com/js/', headless=True, network_idle=True)

# 会话模式:支持广告/域名拦截
with DynamicSession(headless=True, block_ads=True) as session:
    page = session.fetch('https://quotes.toscrape.com/js/')

五、自适应元素追踪(抗网站改版)

Scrapling 独家特性:通过记录元素身份特征(标签、属性、结构、内容等),网站改版后自动重定位元素,大幅降低维护成本。

python

运行

from scrapling.fetchers import Fetcher

# 首次采集:保存元素特征
page = Fetcher.get('https://example.com/products')
products = page.css('.product-card', auto_save=True)

# 网站改版后:自适应重定位元素
page = Fetcher.get('https://example.com/products')
products = page.css('.product-card', adaptive=True)

支持<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">find_similar()</font>方法,可批量匹配页面结构相似元素。

六、Spider 分布式爬取框架

类 Scrapy 设计,支持高并发、分页追踪、断点续爬,支持多会话类型混合使用,按需分配普通请求与隐身浏览器。

python

运行

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(crawldir="./crawl_data").start()
# 数据导出
result.items.to_json("quotes.json")

性能对比

基于 5000 个嵌套元素文本提取测试(100 + 次运行平均值):

表格

框架 / 库耗时 (ms)性能倍率
Scrapling2.021.0x
Parsel/Scrapy2.04~1x
Raw Lxml2.541.25x
PyQuery24.17~12x
BeautifulSoup4 + lxml1584.31~784x

Scrapling 底层基于 lxml,解析性能与 Scrapy 持平,较 BeautifulSoup4 提升近 800 倍。

不适用场景

  1. 超大规模分布式爬取:框架为单机设计,百万级 URL 分布式爬取推荐使用 Scrapy+Scrapy-Redis;
  2. 纯 HTML 解析需求:无需网络请求时,可仅使用<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">scrapling.parser</font>模块,减少依赖体积;
  3. 企业级反爬对抗:对 Akamai、DataDome 等无原生绕过方案,需集成第三方服务;
  4. 底层 HTTP 精细控制:自定义 DNS、HTTP/2 帧、TLS 套件等场景,推荐使用 httpx/curl_cffi。

常见问题

  1. 安装失败:核心原因为网络问题,Chromium (150MB)、Camoufox (80MB) 下载受阻,建议使用代理或手动部署;
  2. 反爬检测失效:确认开启<font style="color:rgb(31, 35, 41);background-color:rgba(0, 0, 0, 0);">solve_cloudflare=True</font>,非 Cloudflare 防护站点无原生绕过能力;
  3. 并发数配置:单 IP 建议≤10 并发,代理池环境可配置 50-100 并发,避免触发频率限制;
  4. 自适应准确率:常规 CSS 改版准确率≥90%,页面大规模重构时准确率下降,关键任务建议人工校验。

总结

  1. Scrapling 通过一体化设计、原生反反爬、自适应定位三大核心能力,彻底简化爬虫开发与维护流程;
  2. 兼容多选择器语法、支持动态渲染、断点续爬,兼顾开发效率与爬取稳定性;
  3. 性能比肩 Scrapy,使用成本更低,适合中小型爬虫项目、长期稳定采集场景。