D4Vinci/Scrapling
GitHub: D4Vinci/Scrapling
Scrapling 是一个具备自适应解析和反爬绕过能力的 Python 爬虫框架,从单次请求到大规模爬取都能胜任。
Stars: 21531 | Forks: 1501
Effortless Web Scraping for the Modern Web
العربيه | Español | Français | Deutsch | 简体中文 | 日本語 | Русский
选择方法 · 获取器 · Spider · 代理轮换 · CLI · MCP
Scrapling 是一个自适应的 Web 抓取框架,处理从单个请求到大规模爬取的所有事务。 它的解析器能从网站变更中学习,并在页面更新时自动重新定位你的元素。它的获取器开箱即即用,能够绕过 Cloudflare Turnstile 等 anti-bot 系统。它的 spider 框架让你只需几行 Python 代码就能扩展到并发、多会话爬取,并支持暂停/恢复和自动代理轮换。一个库,零妥协。 极速爬取,附带实时统计和流式传输。由 Web Scraper 为 Web Scraper 和普通用户打造,每个人都能找到适合自己的功能。 ``` from scrapling.fetchers import Fetcher, AsyncFetcher, StealthyFetcher, DynamicFetcher StealthyFetcher.adaptive = True p = StealthyFetcher.fetch('https://example.com', headless=True, network_idle=True) # Fetch website under the radar! products = p.css('.product', auto_save=True) # Scrape data that survives website design changes! products = p.css('.product', adaptive=True) # Later, if the website structure changes, pass `adaptive=True` to find them! ``` 或者扩展到完整的爬取 ``` from scrapling.spiders import Spider, Response class MySpider(Spider): name = "demo" start_urls = ["https://example.com/"] async def parse(self, response: Response): for item in response.css('.product'): yield {"title": item.css('h2::text').get()} MySpider().start() ``` ## 主要特性 ### Spider — 完整的爬取框架 - 🕷️ **类 Scrapy 的 Spider API**:使用 `start_urls`、异步 `parse` 回调以及 `Request`/`Response` 对象定义 spider。 - ⚡ **并发爬取**:可配置的并发限制、针对特定域名的节流以及下载延迟。 - 🔄 **多会话支持**:在单个 spider 中为 HTTP 请求和隐身无头浏览器提供统一接口 —— 通过 ID 将请求路由到不同的会话。 - 💾 **暂停与恢复**:基于检查点的爬取持久化。按 Ctrl+C 优雅关闭;重启后从断点恢复。 - 📡 **流式模式**:通过 `async for item in spider.stream()` 在抓取项到达时进行流式传输,并附带实时统计 —— 适用于 UI、pipeline 和长时间运行的爬取。 - 🛡️ **被阻请求检测**:自动检测并重试被阻止的请求,支持自定义逻辑。 - 📦 **内置导出**:通过 hook 和你自己的 pipeline 导出结果,或使用内置的 JSON/JSONL,分别调用 `result.items.to_json()` / `result.items.to_jsonl()`。 ### 支持会话的高级网站获取 - **HTTP 请求**:通过 `Fetcher` 类实现快速且隐蔽的 HTTP 请求。可以模拟浏览器的 TLS 指纹、headers,并使用 HTTP/3。 - **动态加载**:通过 `DynamicFetcher` 类获取动态网站,支持完整的浏览器自动化,兼容 Playwright 的 Chromium 和 Google 的 Chrome。 - **Anti-bot 绕过**:通过 `StealthyFetcher` 实现高级隐身能力和指纹欺骗。可以轻松通过自动化绕过所有类型的 Cloudflare Turnstile/Interstitial。 - **会话管理**:通过 `FetcherSession`、`StealthySession` 和 `DynamicSession` 类提供持久会话支持,用于跨请求的 cookie 和状态管理。 - **代理轮换**:内置 `ProxyRotator`,支持跨所有会话类型的循环或自定义轮换策略,以及针对单次请求的代理覆盖。 - **域名拦截**:在基于浏览器的获取器中阻止对特定域名(及其子域名)的请求。 - **异步支持**:所有获取器均提供完整的异步支持,并包含专门的异步会话类。 ### 自适应抓取与 AI 集成 - 🔄 **智能元素追踪**:在网站变更后利用智能相似度算法重新定位元素。 - 🎯 **智能灵活选择**:CSS 选择器、XPath 选择器、基于过滤器的搜索、文本搜索、正则表达式搜索等。 - 🔍 **查找相似元素**:自动定位与已找到元素相似的元素。 - 🤖 **供 AI 使用的 MCP Server**:内置 MCP server,用于 AI 辅助的 Web 抓取和数据提取。该 MCP server 具有强大的自定义功能,利用 Scrapling 在将目标内容传递给 AI(Claude/Cursor 等)之前进行提取,从而通过减少 token 使用量来加快操作速度并降低成本。([演示视频](https://www.youtube.com/watch?v=qyFk3ZNwOxE)) ### 高性能且久经考验的架构 - 🚀 **闪电般的速度**:经过优化的性能,优于大多数 Python 抓取库。 - 🔋 **内存高效**:优化的数据结构和懒加载,实现最小的内存占用。 - ⚡ **快速 JSON 序列化**:比标准库快 10 倍。 - 🏗️ **久经考验**:Scrapling 不仅拥有 92% 的测试覆盖率和完整的类型提示覆盖率,而且在过去一年中每天被数百名 Web Scraper 使用。 ### 对开发者/Web Scraper 友好的体验 - 🎯 **交互式 Web 抓取 Shell**:可选的内置 IPython shell,集成 Scrapling、快捷键和新工具,以加快 Web 抓取脚本的开发,例如将 curl 请求转换为 Scrapling 请求并在浏览器中查看请求结果。 - 🚀 **直接从终端使用**:你可以选择在不编写任何代码的情况下使用 Scrapling 抓取 URL! - 🛠️ **丰富的导航 API**:支持父节点、兄弟节点和子节点导航方法的高级 DOM 遍历。 - 🧬 **增强的文本处理**:内置正则表达式、清理方法和优化的字符串操作。 - 📝 **自动选择器生成**:为任何元素生成健壮的 CSS/XPath 选择器。 - 🔌 **熟悉的 API**:类似于 Scrapy/BeautifulSoup,使用与 Scrapy/Parsel 相同的伪元素。 - 📘 **完整的类型覆盖**:完整的类型提示,提供出色的 IDE 支持和代码补全。每次更改时,整个代码库都会自动使用 **PyRight** 和 **MyPy** 进行扫描。 - 🔋 **现成的 Docker 镜像**:每次发布时,都会自动构建并推送一个包含所有浏览器的 Docker 镜像。 ## 快速入门 让我们快速浏览一下 Scrapling 的功能,不做深入探讨。 ### 基本用法 支持会话的 HTTP 请求 ``` from scrapling.fetchers import Fetcher, FetcherSession with FetcherSession(impersonate='chrome') as session: # Use latest version of Chrome's TLS fingerprint page = session.get('https://quotes.toscrape.com/', stealthy_headers=True) quotes = page.css('.quote .text::text').getall() # 或使用一次性请求 page = Fetcher.get('https://quotes.toscrape.com/') quotes = page.css('.quote .text::text').getall() ``` 高级隐身模式 ``` from scrapling.fetchers import StealthyFetcher, StealthySession with StealthySession(headless=True, solve_cloudflare=True) as session: # Keep the browser open until you finish page = session.fetch('https://nopecha.com/demo/cloudflare', google_search=False) data = page.css('#padded_content a').getall() # 或使用一次性请求风格,它会为此请求打开浏览器,完成后关闭 page = StealthyFetcher.fetch('https://nopecha.com/demo/cloudflare') data = page.css('#padded_content a').getall() ``` 完整的浏览器自动化 ``` from scrapling.fetchers import DynamicFetcher, DynamicSession with DynamicSession(headless=True, disable_resources=False, network_idle=True) as session: # Keep the browser open until you finish page = session.fetch('https://quotes.toscrape.com/', load_dom=False) data = page.xpath('//span[@class="text"]/text()').getall() # XPath selector if you prefer it # 或使用一次性请求风格,它会为此请求打开浏览器,完成后关闭 page = DynamicFetcher.fetch('https://quotes.toscrape.com/') data = page.css('.quote .text::text').getall() ``` ### Spider 构建具有并发请求、多种会话类型和暂停/恢复功能的完整爬虫: ``` from scrapling.spiders import Spider, Request, 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') if next_page: yield response.follow(next_page[0].attrib['href']) result = QuotesSpider().start() print(f"Scraped {len(result.items)} quotes") result.items.to_json("quotes.json") ``` 在单个 spider 中使用多种会话类型: ``` from scrapling.spiders import Spider, Request, Response from scrapling.fetchers import FetcherSession, AsyncStealthySession class MultiSessionSpider(Spider): name = "multi" start_urls = ["https://example.com/"] def configure_sessions(self, manager): 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(): # Route protected pages through the stealth session if "protected" in link: yield Request(link, sid="stealth") else: yield Request(link, sid="fast", callback=self.parse) # explicit callback ``` 通过运行 spider 并使用检查点来暂停和恢复长时间的爬取: ``` QuotesSpider(crawldir="./crawl_data").start() ``` 按 Ctrl+C 优雅地暂停 —— 进度会自动保存。稍后,当你再次启动 spider 时,传递相同的 `crawldir`,它将从停止的地方恢复。 ### 高级解析与导航 ``` from scrapling.fetchers import Fetcher # 丰富的元素选择与导航 page = Fetcher.get('https://quotes.toscrape.com/') # 通过多种选择方法获取 quotes quotes = page.css('.quote') # CSS selector quotes = page.xpath('//div[@class="quote"]') # XPath quotes = page.find_all('div', {'class': 'quote'}) # BeautifulSoup-style # 等同于 quotes = page.find_all('div', class_='quote') quotes = page.find_all(['div'], class_='quote') quotes = page.find_all(class_='quote') # and so on... # 通过文本内容查找元素 quotes = page.find_by_text('quote', tag='div') # 高级导航 quote_text = page.css('.quote')[0].css('.text::text').get() quote_text = page.css('.quote').css('.text::text').getall() # Chained selectors first_quote = page.css('.quote')[0] author = first_quote.next_sibling.css('.author::text') parent_container = first_quote.parent # 元素关系与相似度 similar_elements = first_quote.find_similar() below_elements = first_quote.below_elements() ``` 如果你不想获取网站,可以像下面这样直接使用解析器: ``` from scrapling.parser import Selector page = Selector("...") ``` 它的运作方式完全相同! ### 异步会话管理示例 ``` import asyncio from scrapling.fetchers import FetcherSession, AsyncStealthySession, AsyncDynamicSession async with FetcherSession(http3=True) as session: # `FetcherSession` is context-aware and can work in both sync/async patterns page1 = session.get('https://quotes.toscrape.com/') page2 = session.get('https://quotes.toscrape.com/', impersonate='firefox135') # Async session 使用 async with AsyncStealthySession(max_pages=2) as session: tasks = [] urls = ['https://example.com/page1', 'https://example.com/page2'] for url in urls: task = session.fetch(url) tasks.append(task) print(session.get_pool_stats()) # Optional - The status of the browser tabs pool (busy/free/error) results = await asyncio.gather(*tasks) print(session.get_pool_stats()) ``` ## CLI 与交互式 Shell Scrapling 包含一个强大的命令行界面: [](https://asciinema.org/a/736339) 启动交互式 Web 抓取 shell ``` scrapling shell ``` 无需编程即可将页面直接提取到文件中(默认提取 `body` 标签内的内容)。如果输出文件以 `.txt` 结尾,则提取目标的文本内容。如果以 `.md` 结尾,则是 HTML 内容的 Markdown 表示;如果以 `.html` 结尾,则是 HTML 内容本身。 ``` scrapling extract get 'https://example.com' content.md scrapling extract get 'https://example.com' content.txt --css-selector '#fromSkipToProducts' --impersonate 'chrome' # All elements matching the CSS selector '#fromSkipToProducts' scrapling extract fetch 'https://example.com' content.md --css-selector '#fromSkipToProducts' --no-headless scrapling extract stealthy-fetch 'https://nopecha.com/demo/cloudflare' captchas.html --css-selector '#padded_content a' --solve-cloudflare ``` ## 性能基准测试 Scrapling 不仅功能强大 —— 而且速度极快。以下基准测试将 Scrapling 的解析器与其他流行库的最新版本进行了比较。 ### 文本提取速度测试(5000 个嵌套元素) | # | 库 | 时间 (ms) | 对比 Scrapling | |---|:-----------------:|:---------:|:------------:| | 1 | Scrapling | 2.02 | 1.0x | | 2 | Parsel/Scrapy | 2.04 | 1.01 | | 3 | Raw Lxml | 2.54 | 1.257 | | 4 | PyQuery | 24.17 | ~12x | | 5 | Selectolax | 82.63 | ~41x | | 6 | MechanicalSoup | 1549.71 | ~767.1x | | 7 | BS4 with Lxml | 1584.31 | ~784.3x | | 8 | BS4 with html5lib | 3391.91 | ~1679.1x | ### 元素相似度与文本搜索性能 Scrapling 的自适应元素查找功能显著优于替代方案: | 库 | 时间 (ms) | 对比 Scrapling | |-------------|:---------:|:------------:| | Scrapling | 2.39 | 1.0x | | AutoScraper | 12.45 | 5.209x | ## 安装 Scrapling 需要 Python 3.10 或更高版本: ``` pip install scrapling ``` 此安装仅包含解析器引擎及其依赖项,不包含任何获取器或命令行依赖项。 ### 可选依赖项 1. 如果你要使用以下任何额外功能、获取器或其类,则需要安装获取器的依赖项及其浏览器依赖项,如下所示: pip install "scrapling[fetchers]" scrapling install # 正常安装 scrapling install --force # 强制重新安装 这将下载所有浏览器,以及它们的系统依赖项和指纹操作依赖项。 或者,你可以通过代码而不是运行命令来安装它们,如下所示: from scrapling.cli import install install([], standalone_mode=False) # 正常安装 install(["--force"], standalone_mode=False) # 强制重新安装 2. 额外功能: - 安装 MCP server 功能: pip install "scrapling[ai]" - 安装 shell 功能(Web 抓取 shell 和 `extract` 命令): pip install "scrapling[shell]" - 安装所有内容: pip install "scrapling[all]" 请记住,如果你还没有安装浏览器依赖项,则需要在安装上述任何扩展后使用 `scrapling install` 进行安装。 ### Docker 你还可以使用以下命令从 DockerHub 安装包含所有扩展和浏览器的 Docker 镜像: ``` docker pull pyd4vinci/scrapling ``` 或从 GitHub registry 下载: ``` docker pull ghcr.io/d4vinci/scrapling:latest ``` 此镜像使用 GitHub Actions 和仓库的主分支自动构建和推送。 ## 免责声明 ## 🎓 引用 如果你在研究中使用了我们的库,请使用以下参考文献引用我们: ``` @misc{scrapling, author = {Karim Shoair}, title = {Scrapling}, year = {2024}, url = {https://github.com/D4Vinci/Scrapling}, note = {An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!} } ``` ## 许可证 本作品根据 BSD-3-Clause 许可证授权。 ## 致谢 本项目包含改编自以下项目的代码: - Parsel(BSD 许可证)—— 用于 [translator](https://github.com/D4Vinci/Scrapling/blob/main/scrapling/core/translator.py) 子模块由 Karim Shoair 用 ❤️ 设计与制作。
标签:BSD, ESC4, OSINT, Python, Web Scraping, 二进制发布, 信息提取, 反反爬虫, 命令控制, 开源工具, 数据挖掘, 数据泄露, 数据采集, 无后门, 浏览器自动化, 爬虫框架, 特征检测, 网络调试, 自动化, 自适应, 请求拦截, 逆向工具