返回顶部
s

scrapling高级网页抓取

Advanced web scraping with Scrapling — MCP-native guidance for extraction, crawling, and anti-bot handling. Use via mcporter (MCP) for execution; this skill provides strategy, recipes, and best practices.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
373
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

scrapling

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

执行与指导

任务工具示例
获取页面mcportermcporter call scrapling fetchpage --url URL
使用 CSS 提取
mcporter | mcporter call scrapling cssselect --selector .title::text | | 使用哪个抓取器? | 本技能 | 参见下方抓取器选择指南 | | 反爬策略? | 本技能 | 参见反爬升级阶梯 | | 复杂爬取模式? | 本技能 | 参见爬虫配方 |

抓取器选择指南

┌─────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Fetcher │────▶│ DynamicFetcher │────▶│ StealthyFetcher │
│ (HTTP) │ │ (浏览器/JS) │ │ (反爬) │
└─────────────────┘ └──────────────────┘ └──────────────────┘
最快 JS 渲染 Cloudflare,
静态页面 SPA, React/Vue Turnstile 等

决策树

  1. 1. 静态 HTML? → Fetcher(快 10-100 倍)
  2. 需要执行 JS? → DynamicFetcher
  3. 被屏蔽? → StealthyFetcher
  4. 复杂会话? → 使用 Session 变体

MCP 抓取模式

  • - fetchpage — HTTP 抓取器
  • fetchdynamic — 基于 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

爬虫框架(大规模抓取)

何时使用爬虫 vs 直接抓取:

  • - ✅ 爬虫:10 页以上,需要并发,支持断点续传,代理轮换
  • 直接:1-5 页,快速提取,简单流程

基础爬虫模式

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)

高级:多会话爬虫

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)

爬虫特性

  • - 暂停/恢复:crawldir 参数保存检查点
  • 流式处理:async for item in spider.stream() 实时处理
  • 自动重试:可配置被屏蔽请求的重试
  • 导出:内置 tojson()、tojsonl()

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.findall(div, class=item)

解析器 API(超越 CSS/XPath)

BeautifulSoup 风格方法

python

按属性查找

page.find_all(div, {class: product, data-id: True}) page.findall(div, class=product, id=re.compile(ritem-\\d+))

文本搜索

page.findbytext(Add to Cart, tag=button) page.findbyregex(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.autocssselector() # 返回稳定的 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 = fhttps://example.com/products?page={page_num} ...

下一页按钮

while next_page := response.css(.next a::attr(href)).get(): yield response.follow(next

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 scrapling-yoo-1776292393 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 scrapling-yoo-1776292393 技能

通过命令行安装

skillhub install scrapling-yoo-1776292393

下载

⬇ 下载 scrapling v1.0.0(免费)

文件大小: 14.87 KB | 发布时间: 2026-4-16 15:31

v1.0.0 最新 2026-4-16 15:31
scrapling-yoo 1.0.0

- Initial release providing web scraping guidance, strategies, and best practices for the Scrapling framework integrated with MCP (mcporter/OpenClaw).
- Covers fetcher selection, anti-bot techniques, spider patterns, and adaptive scraping.
- Includes step-by-step quick start instructions for MCP integration and usage.
- Documents core concepts, spider framework usage, parser API, proxy rotation, CLI commands, and common scraping recipes.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部