Scrapling Web Scraping Skill
Use Scrapling to scrape modern websites, including those with anti-bot protection, JavaScript-rendered content, and adaptive element tracking.
When to Use This Skill
- - User asks to scrape a website or extract data from a URL
- Need to bypass Cloudflare, bot detection, or anti-scraping measures
- Need to handle JavaScript-rendered/dynamic content (React, Vue, etc.)
- Website requires login or session management
- Website structure changes frequently (adaptive selectors)
- Need to scrape multiple pages with rate limiting
Commands
All commands use the scrape.py script in this skill's directory.
Basic HTTP Scraping (Fast)
CODEBLOCK0
Use when: Static HTML, no JavaScript, no bot protection
Stealth Mode (Bypass Anti-Bot)
CODEBLOCK1
Use when: Cloudflare protection, bot detection, fingerprinting
Features:
- - Bypasses Cloudflare Turnstile automatically
- Browser fingerprint spoofing
- Headless browser mode
Dynamic/JavaScript Content
CODEBLOCK2
Use when: React/Vue/Angular apps, lazy-loaded content, AJAX
Features:
- - Full Playwright browser automation
- Wait for elements to load
- Network idle detection
Adaptive Selectors (Survives Website Changes)
CODEBLOCK3
Use when: Website frequently redesigns, need robust scraping
How it works:
- - First run: Saves element patterns/structure
- Later runs: Uses similarity algorithms to relocate moved elements
- Auto-updates selector cache
Session Management (Login Required)
CODEBLOCK4
Use when: Content requires authentication, multi-step scraping
Extract Specific Data Types
Text only:
CODEBLOCK5
Markdown:
CODEBLOCK6
Attributes:
CODEBLOCK7
Multiple fields:
CODEBLOCK8
Advanced Options
Proxy support:
CODEBLOCK9
Rate limiting:
CODEBLOCK10
Custom headers:
CODEBLOCK11
Screenshot (for debugging):
CODEBLOCK12
Python API (For Custom Scripts)
You can also use Scrapling directly in Python scripts:
CODEBLOCK13
Output Formats
- - JSON (default): INLINECODE1
- JSONL (streaming): INLINECODE2
- CSV: INLINECODE3
- TXT (text only): INLINECODE4
- MD (markdown): INLINECODE5
- HTML (raw): INLINECODE6
Selector Types
Scrapling supports multiple selector formats:
CSS selectors:
CODEBLOCK14
XPath selectors:
CODEBLOCK15
Pseudo-elements (like Scrapy):
CODEBLOCK16
Combined selectors:
CODEBLOCK17
Troubleshooting
Issue: "Element not found"
- - Try
--dynamic if content is JavaScript-loaded - Use
--wait-for SELECTOR to wait for element - Use
--screenshot to debug what's visible
Issue: "Cloudflare blocking"
- - Use
--stealth mode - Add
--solve-cloudflare flag (enabled by default in stealth) - Try
--delay 2 to slow down requests
Issue: "Login not working"
- - Use
--headless false to see browser interaction - Check credentials are correct
- Website might use CAPTCHA (manual intervention needed)
Issue: "Selector broke after website update"
- - Use
--adaptive mode to auto-relocate elements - Re-run with
--adaptive-save to update saved patterns
Examples
Scrape Hacker News Front Page
CODEBLOCK18
Scrape Protected Site with Login
CODEBLOCK19
Monitor Price Changes
CODEBLOCK20
Scrape Dynamic JavaScript App
CODEBLOCK21
Notes
- - First run: Scrapling downloads browsers (~500MB). This is automatic.
- Sessions: Saved in
sessions/ directory, reusable across runs - Adaptive cache: Saved in
selector_cache.json, auto-updated - Rate limiting: Always respect
robots.txt and add delays for ethical scraping - Legal: Use only on sites you have permission to scrape
Dependencies
Installed automatically when skill is installed:
- - scrapling[all] - Main library with all features
- pyyaml - For config file support
Skill Structure
CODEBLOCK22
Advanced: Custom Python Scripts
For complex scraping tasks, you can create custom Python scripts in this directory:
CODEBLOCK23
Run with:
python custom_scraper.py
Questions? Check Scrapling docs: https://scrapling.readthedocs.io
Scrapling 网页抓取技能
使用 Scrapling 抓取现代网站,包括具有反机器人保护、JavaScript 渲染内容和自适应元素跟踪的网站。
何时使用此技能
- - 用户要求抓取网站或从 URL 提取数据
- 需要绕过 Cloudflare、机器人检测或反爬虫措施
- 需要处理 JavaScript 渲染/动态内容(React、Vue 等)
- 网站需要登录或会话管理
- 网站结构频繁变化(自适应选择器)
- 需要带速率限制地抓取多个页面
命令
所有命令均使用此技能目录中的 scrape.py 脚本。
基本 HTTP 抓取(快速)
bash
python scrape.py \
--url https://example.com \
--selector .product \
--output products.json
使用场景: 静态 HTML,无 JavaScript,无机器人保护
隐身模式(绕过反机器人)
bash
python scrape.py \
--url https://nopecha.com/demo/cloudflare \
--stealth \
--selector #content \
--output data.json
使用场景: Cloudflare 保护、机器人检测、指纹识别
特性:
- - 自动绕过 Cloudflare Turnstile
- 浏览器指纹伪装
- 无头浏览器模式
动态/JavaScript 内容
bash
python scrape.py \
--url https://spa-website.com \
--dynamic \
--selector .loaded-content \
--wait-for .loaded-content \
--output data.json
使用场景: React/Vue/Angular 应用、懒加载内容、AJAX
特性:
- - 完整的 Playwright 浏览器自动化
- 等待元素加载
- 网络空闲检测
自适应选择器(应对网站变化)
bash
首次运行 - 保存选择器模式
python scrape.py \
--url https://example.com \
--selector .product-card \
--adaptive-save \
--output products.json
之后,如果网站结构发生变化
python scrape.py \
--url https://example.com \
--adaptive \
--output products.json
使用场景: 网站频繁改版,需要稳健抓取
工作原理:
- - 首次运行:保存元素模式/结构
- 后续运行:使用相似度算法重新定位移动的元素
- 自动更新选择器缓存
会话管理(需要登录)
bash
登录并保存会话
python scrape.py \
--url https://example.com/dashboard \
--stealth \
--login \
--username user@example.com \
--password password123 \
--session-name my-session \
--selector .protected-data \
--output data.json
重用已保存的会话(无需登录)
python scrape.py \
--url https://example.com/another-page \
--stealth \
--session-name my-session \
--selector .more-data \
--output more_data.json
使用场景: 内容需要身份验证、多步骤抓取
提取特定数据类型
仅文本:
bash
python scrape.py \
--url https://example.com \
--selector .content \
--extract text \
--output content.txt
Markdown:
bash
python scrape.py \
--url https://docs.example.com \
--selector article \
--extract markdown \
--output article.md
属性:
bash
提取 href 链接
python scrape.py \
--url https://example.com \
--selector a.product-link \
--extract attr:href \
--output links.json
多字段:
bash
python scrape.py \
--url https://example.com/products \
--selector .product \
--fields title:.title::text,price:.price::text,link:a::attr(href) \
--output products.json
高级选项
代理支持:
bash
python scrape.py \
--url https://example.com \
--proxy http://user:pass@proxy.com:8080 \
--selector .content
速率限制:
bash
python scrape.py \
--url https://example.com \
--selector .content \
--delay 2 # 请求之间间隔 2 秒
自定义请求头:
bash
python scrape.py \
--url https://api.example.com \
--headers {Authorization: Bearer token123} \
--selector body
截图(用于调试):
bash
python scrape.py \
--url https://example.com \
--stealth \
--screenshot debug.png
Python API(用于自定义脚本)
您也可以直接在 Python 脚本中使用 Scrapling:
python
from scrapling.fetchers import Fetcher, StealthyFetcher, DynamicFetcher
基本 HTTP 请求
page = Fetcher.get(https://example.com)
products = page.css(.product)
for product in products:
title = product.css(.title::text).get()
price = product.css(.price::text).get()
print(f{title}: {price})
隐身模式(绕过反机器人)
page = StealthyFetcher.fetch(https://protected-site.com, headless=True)
data = page.css(.content).getall()
动态内容(完整浏览器)
page = DynamicFetcher.fetch(https://spa-app.com, network_idle=True)
items = page.css(.loaded-item).getall()
会话(登录)
from scrapling.fetchers import StealthySession
with StealthySession(headless=True) as session:
# 登录
login_page = session.fetch(https://example.com/login)
login_page.fill(#username, user@example.com)
login_page.fill(#password, password123)
login_page.click(#submit)
# 访问受保护内容
protected_page = session.fetch(https://example.com/dashboard)
data = protected_page.css(.private-data).getall()
输出格式
- - JSON(默认):--output data.json
- JSONL(流式):--output data.jsonl
- CSV:--output data.csv
- TXT(仅文本):--output data.txt
- MD(Markdown):--output data.md
- HTML(原始):--output data.html
选择器类型
Scrapling 支持多种选择器格式:
CSS 选择器:
bash
--selector .product
--selector div.container > p.text
--selector a[href*=product]
XPath 选择器:
bash
--selector //div[@class=product]
--selector //a[contains(@href, product)]
伪元素(类似 Scrapy):
bash
--selector .product::text # 文本内容
--selector a::attr(href) # 属性值
--selector .price::text::strip # 去除空白后的文本
组合选择器:
bash
--selector .product .title::text # 嵌套元素
故障排除
问题:元素未找到
- - 如果内容是 JavaScript 加载的,尝试使用 --dynamic
- 使用 --wait-for SELECTOR 等待元素
- 使用 --screenshot 调试可见内容
问题:Cloudflare 拦截
- - 使用 --stealth 模式
- 添加 --solve-cloudflare 标志(隐身模式下默认启用)
- 尝试使用 --delay 2 减慢请求速度
问题:登录不起作用
- - 使用 --headless false 查看浏览器交互
- 检查凭据是否正确
- 网站可能使用验证码(需要手动干预)
问题:网站更新后选择器失效
- - 使用 --adaptive 模式自动重新定位元素
- 重新运行 --adaptive-save 以更新保存的模式
示例
抓取 Hacker News 首页
bash
python scrape.py \
--url https://news.ycombinator.com \
--selector .athing \
--fields title:.titleline>a::text,link:.titleline>a::attr(href) \
--output hn_stories.json
抓取需要登录的受保护网站
bash
python scrape.py \
--url https://example.com/data \
--stealth \
--login \
--username user@example.com \
--password secret \
--session-name example-session \
--selector .data-table tr \
--output protected_data.json
监控