🚀 Scrapling
Scrapling 是首個自適應網頁抓取庫,它能從網站的變化中學習並隨之進化。當其他庫因網站結構更新而失效時,Scrapling 能自動重新定位元素,確保抓取程序持續運行,讓你告別與反爬蟲系統的鬥爭,無需在網站更新後重寫選擇器。
🚀 快速開始
基礎用法
from scrapling.fetchers import Fetcher, StealthyFetcher, DynamicFetcher
from scrapling.fetchers import FetcherSession, StealthySession, DynamicSession
with FetcherSession(impersonate='chrome') as session:
page = session.get('https://quotes.toscrape.com/', stealthy_headers=True)
quotes = page.css('.quote .text::text')
page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote .text::text')
with StealthySession(headless=True, solve_cloudflare=True) as session:
page = session.fetch('https://nopecha.com/demo/cloudflare', google_search=False)
data = page.css('#padded_content a')
page = StealthyFetcher.fetch('https://nopecha.com/demo/cloudflare')
data = page.css('#padded_content a')
with DynamicSession(headless=True, disable_resources=False, network_idle=True) as session:
page = session.fetch('https://quotes.toscrape.com/', load_dom=False)
data = page.xpath('//span[@class="text"]/text()')
page = DynamicFetcher.fetch('https://quotes.toscrape.com/')
data = page.css('.quote .text::text')
高級解析與導航
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote')
quotes = page.xpath('//div[@class="quote"]')
quotes = page.find_all('div', {'class': 'quote'})
quotes = page.find_all('div', class_='quote')
quotes = page.find_all(['div'], class_='quote')
quotes = page.find_all(class_='quote')
quotes = page.find_by_text('quote', tag='div')
first_quote = page.css_first('.quote')
quote_text = first_quote.css('.text::text')
quote_text = page.css('.quote').css_first('.text::text')
quote_text = page.css_first('.quote .text').text
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("<html>...</html>")
它的工作方式完全相同!
異步會話管理示例
import asyncio
from scrapling.fetchers import FetcherSession, AsyncStealthySession, AsyncDynamicSession
async with FetcherSession(http3=True) as session:
page1 = session.get('https://quotes.toscrape.com/')
page2 = session.get('https://quotes.toscrape.com/', impersonate='firefox135')
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())
results = await asyncio.gather(*tasks)
print(session.get_pool_stats())
✨ 主要特性
支持會話的高級網站抓取
- HTTP 請求:使用
Fetcher
類進行快速且隱蔽的 HTTP 請求。可以模擬瀏覽器的 TLS 指紋、頭部信息,並使用 HTTP3。
- 動態加載:通過支持 Playwright 的 Chromium、真實 Chrome 和自定義隱身模式的
DynamicFetcher
類,實現全瀏覽器自動化來抓取動態網站。
- 繞過反爬蟲機制:
StealthyFetcher
具有高級隱身功能,使用修改版的 Firefox 和指紋欺騙技術。可以輕鬆通過自動化繞過所有級別的 Cloudflare 的 Turnstile。
- 會話管理:
FetcherSession
、StealthySession
和 DynamicSession
類支持持久會話,用於跨請求的 cookie 和狀態管理。
- 異步支持:所有抓取器都提供完整的異步支持,並配有專門的異步會話類。
自適應抓取與 AI 集成
- 🔄 智能元素跟蹤:使用智能相似性算法在網站更改後重新定位元素。
- 🎯 智能靈活選擇:支持 CSS 選擇器、XPath 選擇器、基於過濾器的搜索、文本搜索、正則表達式搜索等。
- 🔍 查找相似元素:自動定位與已找到元素相似的元素。
- 🤖 可與 AI 配合使用的 MCP 服務器:內置 MCP 服務器,用於 AI 輔助的網頁抓取和數據提取。MCP 服務器具有自定義的強大功能,利用 Scrapling 在將目標內容傳遞給 AI(Claude/Cursor 等)之前進行提取,從而通過減少令牌使用來加快操作速度並降低成本。(演示視頻)
高性能且經過實戰檢驗的架構
- 🚀 閃電般快速:經過優化的性能,超越了大多數 Python 抓取庫。
- 🔋 內存高效:優化的數據結構和惰性加載,佔用的內存極少。
- ⚡ 快速 JSON 序列化:比標準庫快 10 倍。
- 🏗️ 經過實戰檢驗:Scrapling 不僅擁有 92% 的測試覆蓋率和完整的類型提示覆蓋率,而且在過去一年中,每天都有數百名網頁抓取人員在使用它。
對開發者/網頁抓取人員友好的體驗
- 🎯 交互式網頁抓取 shell:可選的內置 IPython shell,集成了 Scrapling,提供快捷方式和新工具,可加快網頁抓取腳本的開發速度,例如將 curl 請求轉換為 Scrapling 請求,並在瀏覽器中查看請求結果。
- 🚀 直接從終端使用:你可以選擇直接使用 Scrapling 來抓取 URL,而無需編寫任何代碼!
- 🛠️ 豐富的導航 API:通過父級、兄弟級和子級導航方法實現高級 DOM 遍歷。
- 🧬 增強的文本處理:內置正則表達式、清理方法和優化的字符串操作。
- 📝 自動選擇器生成:為任何元素生成強大的 CSS/XPath 選擇器。
- 🔌 熟悉的 API:類似於 Scrapy/BeautifulSoup,使用與 Scrapy/Parsel 相同的偽元素。
- 📘 完整的類型覆蓋:完整的類型提示,為 IDE 提供出色的支持和代碼補全功能。
全新的會話架構
Scrapling 0.3 引入了全新的會話系統:
- 持久會話:在多個請求之間保持 cookie、頭部信息和身份驗證。
- 自動會話管理:智能處理會話生命週期,並進行適當的清理。
- 會話繼承:所有抓取器都支持一次性請求和持久會話使用。
- 併發會話支持:同時運行多個隔離的會話。
📦 安裝指南
Scrapling 需要 Python 3.10 或更高版本:
pip install scrapling
從 v0.3.2 開始,此安裝僅包括解析引擎及其依賴項,不包括任何抓取器或命令行依賴項。
可選依賴項
-
如果你打算使用以下任何額外功能、抓取器或它們的類,則需要安裝抓取器的依賴項,然後使用以下命令安裝瀏覽器依賴項:
pip install "scrapling[fetchers]"
scrapling install
這將下載所有瀏覽器及其系統依賴項和指紋操作依賴項。
-
額外功能:
- 安裝 MCP 服務器功能:
pip install "scrapling[ai]"
- 安裝 shell 功能(網頁抓取 shell 和
extract
命令):pip install "scrapling[shell]"
- 安裝所有功能:
pip install "scrapling[all]"
不要忘記在安裝這些額外功能後(如果你還沒有安裝),使用 scrapling install
安裝瀏覽器依賴項。
💻 使用示例
基礎用法
from scrapling.fetchers import Fetcher, AsyncFetcher, StealthyFetcher, DynamicFetcher
StealthyFetcher.adaptive = True
page = StealthyFetcher.fetch('https://example.com', headless=True, network_idle=True)
print(page.status)
200
products = page.css('.product', auto_save=True)
products = page.css('.product', adaptive=True)
高級用法
from scrapling.fetchers import Fetcher, StealthyFetcher, DynamicFetcher
from scrapling.fetchers import FetcherSession, StealthySession, DynamicSession
with FetcherSession(impersonate='chrome') as session:
page = session.get('https://quotes.toscrape.com/', stealthy_headers=True)
quotes = page.css('.quote .text::text')
page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote .text::text')
with StealthySession(headless=True, solve_cloudflare=True) as session:
page = session.fetch('https://nopecha.com/demo/cloudflare', google_search=False)
data = page.css('#padded_content a')
page = StealthyFetcher.fetch('https://nopecha.com/demo/cloudflare')
data = page.css('#padded_content a')
with DynamicSession(headless=True, disable_resources=False, network_idle=True) as session:
page = session.fetch('https://quotes.toscrape.com/', load_dom=False)
data = page.xpath('//span[@class="text"]/text()')
page = DynamicFetcher.fetch('https://quotes.toscrape.com/')
data = page.css('.quote .text::text')
📚 詳細文檔
Scrapling v0.3 包含一個強大的命令行界面:

scrapling shell
scrapling extract get 'https://example.com' content.md
scrapling extract get 'https://example.com' content.txt --css-selector '#fromSkipToProducts' --impersonate 'chrome'
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
⚠️ 重要提示
還有許多其他功能,但我們希望保持此頁面簡潔,例如 MCP 服務器和交互式網頁抓取 shell。請查看完整文檔 此處
🔧 技術細節
文本提取速度測試(5000 個嵌套元素)
# |
庫 |
時間 (ms) |
與 Scrapling 相比 |
1 |
Scrapling |
1.92 |
1.0x |
2 |
Parsel/Scrapy |
1.99 |
1.036x |
3 |
Raw Lxml |
2.33 |
1.214x |
4 |
PyQuery |
20.61 |
~11x |
5 |
Selectolax |
80.65 |
~42x |
6 |
BS4 with Lxml |
1283.21 |
~698x |
7 |
MechanicalSoup |
1304.57 |
~679x |
8 |
BS4 with html5lib |
3331.96 |
~1735x |
元素相似性和文本搜索性能
Scrapling 的自適應元素查找功能明顯優於其他替代方案:
庫 |
時間 (ms) |
與 Scrapling 相比 |
Scrapling |
1.87 |
1.0x |
AutoScraper |
10.24 |
5.476x |
所有基準測試均為 100 多次運行的平均值。有關方法,請參閱 benchmarks.py
📄 許可證
本項目採用 BSD-3-Clause 許可證。
致謝
本項目包含改編自以下項目的代碼:
感謝與參考
由 Karim Shoair 用心設計與打造。