详细分析 ▾
运行时依赖
版本
重命名显示名称 + 将技能元数据对齐至 scrapling-mcp(slug 不变)
安装命令
点击复制技能文档
# Scrapling MCP — 网页抓取指南 > 指导层 + MCP 集成 > 使用本技能获取策略与模式。执行时,通过 mcporter 调用 Scrapling 的 MCP 服务器。 ## 快速开始(MCP) ### 1. 安装支持 MCP 的 Scrapling ``bash pip install scrapling[mcp] # 或完整安装: pip install scrapling[mcp,playwright] python -m playwright install chromium ` ### 2. 添加到 OpenClaw MCP 配置 `json { "mcpServers": { "scrapling": { "command": "python", "args": ["-m", "scrapling.mcp"] } } } ` ### 3. 通过 mcporter 调用 ` mcporter call scrapling fetch_page --url "https://example.com" ` ## 执行与指导 | 任务 | 工具 | 示例 | |------|------|---------| | 拉取页面 | mcporter | mcporter call scrapling fetch_page --url URL | | 用 CSS 提取 | mcporter | mcporter call scrapling css_select --selector ".title::text" | | 选哪个 fetcher? | 本技能 | 见下方“Fetcher 选择指南” | | 反爬策略? | 本技能 | 见“反爬升级阶梯” | | 复杂爬取模式? | 本技能 | 见“Spider 配方” | ## Fetcher 选择指南 ` ┌─────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ Fetcher │────▶│ DynamicFetcher │────▶│ StealthyFetcher │ │ (HTTP) │ │ (浏览器/JS) │ │ (反爬) │ └─────────────────┘ └──────────────────┘ └──────────────────┘ 最快 JS 渲染 Cloudflare、静态页面 SPA、React/Vue Turnstile 等 ` ### 决策树 1. 静态 HTML? → Fetcher(快 10–100 倍) 2. 需要执行 JS? → DynamicFetcher 3. 被拦截? → StealthyFetcher 4. 复杂会话? → 使用 Session 变体 ### MCP 抓取模式 - fetch_page — HTTP fetcher - fetch_dynamic — 基于浏览器的 Playwright - fetch_stealthy — 反爬绕过模式 ## 反爬升级阶梯 ### 第 1 级:礼貌 HTTP `python # MCP 调用:带选项的 fetch_page { "url": "https://example.com", "headers": {"User-Agent": "..."}, "delay": 2.0 } ` ### 第 2 级:会话保持 `python # 跨请求保留 cookie/状态 FetcherSession(impersonate="chrome") # TLS 指纹伪装 ` ### 第 3 级:隐身模式 `python # MCP:fetch_stealthy StealthyFetcher.fetch( url, headless=True, solve_cloudflare=True, # 自动解 Turnstile network_idle=True ) ` ### 第 4 级:代理轮换 见 references/proxy-rotation.md ## 自适应抓取(反脆弱) Scrapling 可在网站改版后存活,使用自适应选择器: `python # 首次运行 — 保存指纹 products = page.css('.product', auto_save=True) # 后续运行 — DOM 变化时自动重定位 products = page.css('.product', adaptive=True) ` MCP 用法: ` mcporter call scrapling css_select \\ --selector ".product" \\ --adaptive true \\ --auto-save true ` ## Spider 框架(大规模爬取) 何时用 Spider 而非直接抓取: - ✅ Spider:10+ 页面、需并发、可恢复、代理轮换 - ✅ 直接:1–5 页面、快速提取、流程简单 ### 基础 Spider 模式 `python from scrapling.spiders import Spider, Response class ProductSpider(Spider): name = "products" start_urls = ["https://example.com/products"] concurrent_requests = 10 download_delay = 1.0 async def parse(self, response: Response): for product in response.css('.product'): yield { "name": product.css('h2::text').get(), "price": product.css('.price::text').get(), "url": response.url } # 跟随分页 next_page = response.css('.next a::attr(href)').get() if next_page: yield response.follow(next_page) # 带恢复能力启动 result = ProductSpider(crawldir="./crawl_data").start() result.items.to_jsonl("products.jsonl") ` ### 高级:多会话 Spider `python 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(): if "/protected/" in link: yield Request(link, sid="stealth") else: yield Request(link, sid="fast") ` ### Spider 特性 - 暂停/恢复:crawldir 参数保存检查点 - 流式:async for item in spider.stream() 实时处理 - 自动重试:被拦截时配置重试 - 导出:内置 to_json()、to_jsonl() ## CLI 与交互式 Shell ### 终端提取(零代码) `bash # 提取为 markdown scrapling extract get 'https://example.com' content.md # 提取特定元素 scrapling extract get 'https://example.com' content.txt \\ --css-selector '.article' \\ --impersonate 'chrome' # 隐身模式 scrapling extract stealthy-fetch 'https://protected.com' content.md \\ --no-headless \\ --solve-cloudflare ` ### 交互式 Shell `bash scrapling shell # 在 shell 内: >>> page = Fetcher.get('https://example.com') >>> page.css('h1::text').get() >>> page.find_all('div', class_='item') ` ## Parser API(超越 CSS/XPath) ### BeautifulSoup 风格方法 `python # 按属性查找 page.find_all('div', {'class': 'product', 'data-id': True}) page.find_all('div', class_='product', id=re.compile(r'item-\d+')) # 文本搜索 page.find_by_text('Add to Cart', tag='button') page.find_by_regex(r'\$\d+\.\d{2}') # 导航 first = page.css('.product')[0] parent = first.parent siblings = first.next_siblings children = first.children # 相似元素 similar = first.find_similar() # 视觉/结构相似 below = first.below_elements() # DOM 下方元素 ` ### 自动生成选择器 `python # 为任意元素生成稳定选择器 element = page.css('.product')[0] selector = element.auto_css_selector() # 返回稳定 CSS 路径 xpath = element.auto_xpath() ` ## 代理轮换 `python from scrapling.spiders import ProxyRotator # 循环轮换 rotator = ProxyRotator([ "http://proxy1:8080", "http://proxy2:8080", "http://user:pass@proxy3:8080" ], strategy="cyclic") # 与任意会话配合使用 with FetcherSession(proxy=rotator.next()) as session: page = session.get('https://example.com') ` ## 常见配方 ### 分页模式 `python # 页码 for page_num in range(1, 11): url = f"https://example.com/products?page={page_num}" ... # 下一页按钮 while next_page := response.css('.next a::attr(href)').get(): yield response.follow(next_page) # 无限滚动(DynamicFetcher) with DynamicSession() as session: page = session.fetch(url) page.scroll_to_bottom() items = page.css('.item').getall() ` ### 登录会话 `python with StealthySession(headless=False) as session: # 登录 login_page = session.fetch('https://example.com/login') login_page.fill('input[name="username"]', 'user') login_page.fill('input[name="password"]', 'pass') login_page.click('button[type="submit"]') # 现在会话已带 cookie protected_page = session.fetch('https://example.com/dashboard') ` ### Next.js 数据提取 `python # 从 __NEXT_DATA__ 提取 JSON import json import re next_data = json.loads( re.search( r'__NEXT_DATA__" type="application/json">(.*?)', page.html_content, re.S ).group(1) ) props = next_data['props']['pageProps'] ` ## 输出格式 `python # JSON(美化) result.items.to_json('output.json') # JSONL(流式,每行一条) result.items.to_jsonl('output.jsonl') # Python 对象 for item in result.items: print(item['title']) ` ## 性能提示 1. 优先使用 HTTP fetcher — 比浏览器快 10–100 倍 2. 伪装浏览器 — impersonate='chrome' 进行 TLS 指纹伪装 3. 支持 HTTP/3 — FetcherSession(http3=True) 4. 限制资源 — Dynamic/Stealthy 中 disable_resources=True 5. 连接池 — 跨请求复用会话 ## 安全准则(务必遵守) - 仅抓取已获授权的内容 - 尊重 robots.txt 与 ToS - 大规模爬取时增加延迟(download_delay) - 未经同意勿绕过付费墙或认证 - 绝不抓取个人/敏感数据 ## 参考资料 - references/mcp-setup.md — 详细 MCP 配置 - references/anti-bot.md — 反爬处理策略 - references/proxy-rotation.md — 代理设置与轮换 - references/spider-recipes.md — 高级爬取模式 - references/api-reference.md — 快速 API 参考 - references/links.md — 官方文档链接 ## 脚本 - scripts/scrapling_scrape.py — 一次性快速提取 - scripts/scrapling_smoke_test.py` — 测试连通性与反爬指标