Scrapling爬虫框架简单使用
1 简单介绍
Scrapling由 D4Vinci(Karim Shoair)开发的开源自适应 Web 爬虫框架,专为应对现代复杂网络环境而生的自适应 Web 爬虫框架,旨在为开发者提供从单次轻量请求到大规模并发爬取的全场景解决方案。其核心优势在于通过智能化的架构设计,彻底解决了传统爬虫在网页结构变更和反爬虫对抗中面临的维护痛点。
# Github
https://github.com/D4Vinci/Scrapling
# 官方网站
https://scrapling.readthedocs.io/en/latest/
-
内置反爬虫绕过,包括TLS 指纹模拟(JA3 指纹伪装)、浏览器指纹随机化、人类行为模拟(鼠标移动、滚动、点击延迟)、自动处理 Cloudflare Turnstile / Interstitial 等验证;
-
AI 与 MCP 集成,内置 MCP Server,可与 Claude、Cursor 等 AI 工具无缝对接;
-
多种抓取器,包括Fetcher、DynamicFetcher、StealthyFetcher
| 抓取器 | 适用场景 | 特点 |
|---|---|---|
| Fetcher | 简单静态页面 | 轻量 HTTP 请求,TLS 指纹伪装、HTTP/3 支持,速度最快 |
| DynamicFetcher | JS 动态渲染页面 | 基于 Playwright/Chrome,支持网络空闲检测、资源拦截 |
| StealthyFetcher | 有反爬保护的网站 | 基于 Camoufox 反指纹引擎,零配置绕过 Cloudflare Turnstile |
安装软件包
pip install scrapling[all]
2 采集json数据
在Python环境中,安装Playwright的依赖支持
patchright install
Python代码
下面的代码,不是直接绕过cloudflare验证,而是多次自动验证;如果,通不过验证,依旧获取不到数据。
from scrapling.fetchers import StealthyFetcher
# 自动解析Cloudflare
page = StealthyFetcher.fetch(
url="https://openalex.org/",
solve_cloudflare=True,
block_webrtc=True,
real_chrome=True,
hide_canvas=True,
google_search=True,
)
print(page.markdown())
3 下载PDF数据
下载数据的地址
https://www.nature.com/articles/s41586-024-07487-w.pdf
Python代码,可以直接下载数据
import requests
from scrapling import Fetcher
# 网址
base_url = "https://www.nature.com"
paper_url = "/articles/s41586-024-07487-w"
# 1. 用 Scrapling 抓取目标网页
fetcher = Fetcher()
page = fetcher.get(base_url+paper_url)
# 2. 用 CSS 选择器找到所有 PDF 链接
pdf_links = page.css('a[href$=".pdf"]')
for link in pdf_links:
# 获取PDF链接
pdf_url = link.attrib.get('href')
# 判断是否是PDF链接
if pdf_url:
# 判断是否为网址
if "https://" not in pdf_url:
# 合并地址
pdf_url = base_url + pdf_url
print(pdf_url)
# 3. 用 requests 下载 PDF 文件
response = requests.get(pdf_url)
if response.status_code == 200:
file_name = pdf_url.split('/')[-1]
print(file_name)
# 合并地址
file_path = "./download/" + file_name
with open(file_path, 'wb') as f:
f.write(response.content)
print(f"已下载: {file_name}")
5621



被折叠的 条评论
为什么被折叠?



