恒美微站
首页
关于我们
建站服务
主题模板
案例展示
资讯中心
联系我们
手把手跑通 Scrapling:从反检测请求到整站爬取
首页
资讯中心
/
手把手跑通 Scrapling:从反检测请求到整站爬取
手把手跑通 Scrapling:从反检测请求到整站爬取
发布时间:2026/8/29 22:55:23
手把手跑通 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_requests5的并发是非阻塞调度不占线程。硬仗留给两档浏览器。DynamicSession走 Playwright 跑 JS 渲染StealthySession换用打了隐身补丁的 Patchright还多一个solve_cloudflareTrue自动解 Cloudflare 挑战。选档就一条规则页面能直接渲染出内容就用静态档渲染不出来再升一档。自适应选择器记得住元素特征。解析时传adaptiveTrueScrapling 会把元素特征指纹存进本地 SQLite下次网站改了 class 名或 DOM 结构它按特征重新匹配同一元素你的脚本一行不用改。上面这张架构图同时展示了 Spider 引擎的分工Scheduler 负责请求分发会话层管 cookies 和代理Checkpoint 机制让中断的任务能接着上次跑不会白抓。 跑通三个真实场景三个最常用的模式代码都能直接跑每段不超过 15 行。场景一分页列表批量采集。抓 10 页时用一个 sessionTLS 指纹和 cookies 全程复用不用每页重握手from scrapling.fetchers import FetcherSession with FetcherSession(impersonatechrome) as session: for i in range(1, 11): page session.get(fhttps://quotes.toscrape.com/page/{i}/) quotes page.css(.quote .text::text).getall() print(f第 {i} 页: {len(quotes)} 条 (HTTP {page.status}))场景二JS 渲染、带懒加载的列表页。静态请求只能拿到空壳得开真浏览器。disable_resourcesTrue会跳过图片和字体渲染时间明显下降调试期把headlessFalse窗口打开能直观看到浏览器在干嘛from scrapling.fetchers import DynamicSession with DynamicSession(headlessTrue, disable_resourcesTrue) 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, indentTrue) 我踩过的坑和一句解法前几条几乎每个新手都会撞上都是一句话的事报ModuleNotFoundError: No module named curl_cffi→ 只装了基础包没装取数器 → 换pip install scrapling[fetchers]再跑一次scrapling install。浏览器报 No browser found→ 浏览器和系统依赖没装 →scrapling install --force强制重装。静态页面一律 403→ 站点在校验 TLS/JA3 指纹普通请求被识破 →impersonatechrome伪装成 Chrome。撞见 Cloudflare 挑战页Turnstile→ 无头指纹暴露常规浏览器档过不去 →StealthySession(headlessTrue, solve_cloudflareTrue)。动态页采集偏慢→ 页面在加载大量图片字体 →disable_resourcesTrue跳过非关键资源。从开头 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),仅供参考