手把手跑通 Scrapling:从反检测请求到整站爬取
【免费下载链接】Scrapling🕷️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapling
爬虫爬到第二页被 403,换无头 Chrome 又被挑战页拦下,对方网站换个 class 名选择器就全挂了。Scrapling 是套自适应的爬虫框架,一次解决这三件事:请求伪装成真实浏览器的 TLS 指纹、Cloudflare 挑战一行代码解开、元素特征被记住后结构变化也能重新定位。
⚡ 装好依赖,发出第一个请求
要求 Python 3.10+。两条命令装好,第二条会把浏览器和指纹依赖一起下下来:
pip install "scrapling[fetchers]" scrapling install注意pip install scrapling(不带中括号)只装了解析引擎,import 任何 fetcher 都会直接报ModuleNotFoundError;只解析本地 HTML 的话它才够用。也可以克隆源码装:
git clone https://gitcode.com/GitHub_Trending/sc/Scrapling cd Scrapling && pip install -e .装完用 5 行代码拿第一个结果:
from scrapling.fetchers import Fetcher page = Fetcher.get("https://quotes.toscrape.com/") print(page.css("h1::text").get()) print(page.css(".quote .text::text").getall()[:3])返回的 response 是Selector对象,css、xpath、find_all、find_by_text 都能直接用。第一条打印页面主标题,第二条打印前三条语录——看到输出就说明环境通了。库里一共三档取数器:纯 HTTP 的Fetcher、跑浏览器的DynamicFetcher/StealthyFetcher,以及各自对应的长连接Session类。单次请求用 Fetcher 档,批量抓用 Session 档。
搞懂它为什么快、为什么不被封
表面看三档请求接口统一,真正值钱的是里面这三层机制:
- TLS 层伪装,不用起浏览器。
Fetcher基于 curl_cffi,复刻 Chrome 的 TLS/JA3 握手指纹,纯 HTTP 请求在 WAF 眼里就是真浏览器。所以大多数静态页面完全不用开浏览器,内存和速度都远好于无头 Chrome。整个请求层建在 asyncio 上,concurrent_requests=5的并发是非阻塞调度,不占线程。 - 硬仗留给两档浏览器。
DynamicSession走 Playwright 跑 JS 渲染;StealthySession换用打了隐身补丁的 Patchright,还多一个solve_cloudflare=True自动解 Cloudflare 挑战。选档就一条规则:页面能直接渲染出内容就用静态档,渲染不出来再升一档。 - 自适应选择器记得住元素特征。解析时传
adaptive=True,Scrapling 会把元素特征指纹存进本地 SQLite;下次网站改了 class 名或 DOM 结构,它按特征重新匹配同一元素,你的脚本一行不用改。
上面这张架构图同时展示了 Spider 引擎的分工:Scheduler 负责请求分发,会话层管 cookies 和代理,Checkpoint 机制让中断的任务能接着上次跑,不会白抓。
📊 跑通三个真实场景
三个最常用的模式,代码都能直接跑,每段不超过 15 行。
场景一:分页列表批量采集。抓 10 页时用一个 session,TLS 指纹和 cookies 全程复用,不用每页重握手:
from scrapling.fetchers import FetcherSession with FetcherSession(impersonate="chrome") as session: for i in range(1, 11): page = session.get(f"https://quotes.toscrape.com/page/{i}/") quotes = page.css(".quote .text::text").getall() print(f"第 {i} 页: {len(quotes)} 条 (HTTP {page.status})")场景二:JS 渲染、带懒加载的列表页。静态请求只能拿到空壳,得开真浏览器。disable_resources=True会跳过图片和字体,渲染时间明显下降;调试期把headless=False,窗口打开能直观看到浏览器在干嘛:
from scrapling.fetchers import DynamicSession with DynamicSession(headless=True, disable_resources=True) as session: page = session.fetch("https://quotes.toscrape.com/js/") print(page.css(".quote .text::text").getall()[:3])场景三:整站爬取 + 结构化导出。Spider 引擎自动跟分页、并发抓,最后to_json一行落盘,断点续抓由引擎的 checkpoint 兜底:
from scrapling.spiders import Spider, Response class QuotesSpider(Spider): name = "quotes" start_urls = ["https://quotes.toscrape.com/"] concurrent_requests = 5 # 5 页并发 async def parse(self, response: Response): for quote in response.css(".quote"): yield {"text": quote.css(".text::text").get()} if nxt := response.css(".next a"): # 自动跟"Next"翻页 yield response.follow(nxt[0].attrib["href"]) result = QuotesSpider().start() result.items.to_json("quotes.json", indent=True)🔧 我踩过的坑和一句解法
前几条几乎每个新手都会撞上,都是一句话的事:
- 报
ModuleNotFoundError: No module named 'curl_cffi'→ 只装了基础包没装取数器 → 换pip install "scrapling[fetchers]"再跑一次scrapling install。 - 浏览器报 "No browser found"→ 浏览器和系统依赖没装 →
scrapling install --force强制重装。 - 静态页面一律 403→ 站点在校验 TLS/JA3 指纹,普通请求被识破 →
impersonate="chrome"伪装成 Chrome。 - 撞见 Cloudflare 挑战页(Turnstile)→ 无头指纹暴露,常规浏览器档过不去 →
StealthySession(headless=True, solve_cloudflare=True)。 - 动态页采集偏慢→ 页面在加载大量图片字体 →
disable_resources=True跳过非关键资源。
从开头 3 行Fetcher.get到整站爬取自动导出 JSON,这就是最短路径。全部参数列表和自适应选择器的存储细节,都在官方文档里。
【免费下载链接】Scrapling🕷️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapling
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考