网页抓取框架,支持反爬虫绕过、自适应解析、会话与代理管理、大规模爬取和动态内容提取
Scrapling是一项面向实际任务的技能,主要用于自适应Web爬虫框架,能过反爬、能大规模爬取、网站改版不崩;Quick Start;
Fetcher;普通HTTP请求;最快,适合静态页面;StealthyFetcher;过反爬,过Cloudflare;DynamicFetcher;浏览器模式;JS渲染,SPA页面;
Element Se。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
自适应Web爬虫框架,能过反爬、能大规模爬取、网站改版不崩。
# 基础安装(仅解析器) pip install scrapling # 完整安装(含fetchers和浏览器) pip install "scrapling[all]" scrapling install # 或单独安装功能 pip install "scrapling[fetchers]" # 抓取功能 pip install "scrapling[ai]" # MCP服务 pip install "scrapling[shell]" # 交互式shell
from scrapling.fetchers import Fetcher
page = Fetcher.get('https://quotes.toscrape.com/')
quotes = page.css('.quote .text::text').getall()
print(quotes)
from scrapling.fetchers import StealthyFetcher
# 自动过Cloudflare Turnstile
page = StealthyFetcher.fetch(
'https://目标网站',
headless=True,
solve_cloudflare=True
)
data = page.css('.content::text').getall()
from scrapling.fetchers import DynamicFetcher
# 完整浏览器渲染
page = DynamicFetcher.fetch(
'https://spa网站',
headless=True,
network_idle=True # 等网络请求完成
)
| Fetcher | 用途 | 特点 |
|---|---|---|
Fetcher |
普通HTTP请求 | 最快,适合静态页面 |
StealthyFetcher |
隐身模式 | 过反爬,过Cloudflare |
DynamicFetcher |
浏览器模式 | JS渲染,SPA页面 |
page = Fetcher.get('https://example.com')
# CSS选择器
items = page.css('.item')
title = page.css('h1::text').get()
titles = page.css('h2::text').getall()
# XPath
items = page.xpath('//div[@class="item"]')
# BeautifulSoup风格
items = page.find_all('div', class_='item')
items = page.find_by_text('关键词', tag='div')
# 链式选择
quote_text = page.css('.quote')[0].css('.text::text').get()
# 导航
first = page.css('.item')[0]
parent = first.parent
sibling = first.next_sibling
similar = first.find_similar() # 找相似元素
from scrapling.fetchers import FetcherSession, StealthySession
# 保持会话(cookie复用)
with StealthySession(headless=True, solve_cloudflare=True) as session:
page1 = session.fetch('https://example.com/login')
page2 = session.fetch('https://example.com/dashboard') # 已登录状态
# 异步Session
from scrapling.fetchers import AsyncStealthySession
async with AsyncStealthySession(headless=True) as session:
page = await session.fetch('https://example.com')
from scrapling.spiders import Spider, Response
class MySpider(Spider):
name = "products"
start_urls = ["https://shop.example.com/"]
concurrent_requests = 10 # 并发数
async def parse(self, response: Response):
for item in response.css('.product'):
yield {
"title": item.css('h2::text').get(),
"price": item.css('.price::text').get(),
}
# 翻页
next_page = response.css('.next a::attr(href)').get()
if next_page:
yield response.follow(next_page)
# 运行
result = MySpider().start()
print(f"爬取了 {len(result.items)} 条")
# 导出
result.items.to_json("output.json")
result.items.to_jsonl("output.jsonl")
# 指定crawl目录,支持暂停/恢复 MySpider(crawldir="./crawl_data").start() # Ctrl+C 暂停,再次运行从断点继续
from scrapling.spiders import Spider, Request
from scrapling.fetchers import FetcherSession, AsyncStealthySession
class MultiSpider(Spider):
name = "multi"
def configure_sessions(self, manager):
# 普通请求 - 快
manager.add("fast", FetcherSession(impersonate="chrome"))
# 隐身请求 - 过反爬
manager.add("stealth", AsyncStealthySession(headless=True), lazy=True)
async def parse(self, response):
for link in response.css('a::attr(href)').getall():
if "protected" in link:
yield Request(link, sid="stealth") # 用隐身session
else:
yield Request(link, sid="fast") # 用快速session
网站改版后自动重新定位元素:
# 首次爬取,保存元素特征
products = page.css('.product', auto_save=True)
# 网站改版后,用adaptive=True自动重新定位
products = page.css('.product', adaptive=True)
from scrapling.fetchers import StealthyFetcher, ProxyRotator
proxies = ProxyRotator([
"http://proxy1:8080",
"http://proxy2:8080",
])
page = StealthyFetcher.fetch(
'https://example.com',
proxy=proxies.next()
)
# 交互式shell scrapling shell # 直接抓取(不用写代码) scrapling extract get 'https://example.com' output.md scrapling extract stealthy-fetch 'https://protected.com' output.html --solve-cloudflare # 安装浏览器 scrapling install scrapling install --force
让Claude/Cursor直接调Scrapling爬数据:
pip install "scrapling[ai]" # 启动MCP服务 scrapling mcp
配置到Claude Desktop的config:
{
"mcpServers": {
"scrapling": {
"command": "scrapling",
"args": ["mcp"]
}
}
}
from scrapling.fetchers import StealthyFetcher
page = StealthyFetcher.fetch('https://item.jd.com/12345.html', headless=True)
price = page.css('.price::text').get()
title = page.css('.sku-name::text').get()
from scrapling.spiders import Spider, Response
class JobsSpider(Spider):
name = "jobs"
start_urls = ["https://www.zhipin.com/job_detail/?query=Python"]
async def parse(self, response: Response):
for job in response.css('.job-list li'):
yield {
"title": job.css('.job-name::text').get(),
"salary": job.css('.salary::text').get(),
"company": job.css('.company-name::text').get(),
}
from scrapling.fetchers import Fetcher
import json
def check_competitor(url):
page = Fetcher.get(url)
return {
"products": len(page.css('.product')),
"price_range": page.css('.price::text').getall(),
"updated": page.css('.update-time::text').get(),
}
scrapling shell调试选择器concurrent_requests别太高,容易被封crawldir相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章