web-scraper

$npx mdskill add seaworld008/Commonly-used-high-value-skills/web-scraper

Extract structured data from websites using configurable scraping workflows.

  • Scrapes large-scale data from public websites lacking sufficient APIs.
  • Uses Playwright, Puppeteer, BeautifulSoup, and proxy pools for extraction.
  • Selects parsing strategy based on page structure and anti-bot measures.
  • Returns structured JSON data or triggers alerts on content changes.

SKILL.md

.github/skills/web-scraperView on GitHub ↗
---
name: web-scraper
description: 'Use when users need webpage scraping, structured data extraction, crawling strategy, anti-bot handling, selector design, or repeatable web data collection workflows.'
zh_description: "用于网页抓取、结构化数据提取、爬取策略、选择器设计和反爬应对。"
version: "1.0.0"
author: "seaworld008"
source: "in-house"
source_url: ""
tags: '["automation", "scraper", "web", "workflow"]'
created_at: "2026-03-27"
updated_at: "2026-06-29"
quality: 4
complexity: "intermediate"
---

# Web Scraper

## 触发条件
- 当需要从第三方公开网站获取大规模数据(如价格监控、社交媒体趋势、新闻聚合)时。
- 在没有官方 API 或官方 API 限制过多、功能不足的情况下,需要直接从 HTML 中提取结构化信息。
- 需要自动化执行网页交互任务(如自动登录、表单填写、文件下载)时。
- 建立行业数据库、训练 AI 模型或进行市场调研需要大量原始网页数据支撑时。
- 需要实时监控网页内容变化(如库存变动、价格降价提醒)并触发警报时。

## 核心能力

### 1. HTTP 请求与会话管理 (Requests & Sessions)
- **请求头 (Headers) 伪装**: 模拟真实浏览器行为,随机切换 User-Agent, Referer, Accept-Language。
- **Cookie 维持**: 使用 Session 对象自动处理 Cookie 传递,维持登录态。
- **代理池 (Proxy Pool)**: 动态切换 IP 地址,防止因单一 IP 频繁访问导致的封禁。
- **重试与退避**: 实现自定义 Retry 策略,处理网络波动或短暂的服务端拒绝。

### 2. 多样化解析与提取技术 (Parsing & Extraction)
- **CSS Selectors & XPath**: 利用树状结构精准定位数据。
- **正则表达式 (Regex)**: 提取 HTML 或 JS 脚本中非结构化的文本数据(如动态变量)。
- **BeautifulSoup/Lxml**: Python 经典的 DOM 解析方案。
- **Schema 提取**: 自动识别网页中的 Schema.org 结构化数据(JSON-LD/Microdata)。

### 3. 处理动态 JS 渲染 (Modern Browsing)
- **无头浏览器 (Headless Browsers)**: 
  - **Playwright**: 现代、快速且支持多浏览器的自动化方案。
  - **Puppeteer**: Google Chrome 官方提供的 Node.js 库。
- **等待机制**: 精确控制 `waitForSelector`, `waitForResponse`, `waitForTimeout` 以确保数据加载完成。
- **事件模拟**: 模拟点击 (Click)、滚动 (Scroll)、输入 (Type) 等用户行为触发 AJAX 加载。

### 4. 深度反爬应对策略 (Anti-scraping Evasion)
- **速率限制 (Rate Limiting)**: 实现分布式爬虫的并发控制,模仿人类浏览节奏(设置随机休眠)。
- **浏览器指纹 (Fingerprinting)**: 隐藏或伪造 WebGL, Canvas, 字体等特征,逃避指纹识别系统。
- **验证码 (Captcha) 处理**: 
  - 自动规避策略(如不触发验证码)。
  - 第三方识别服务(如 2Captcha, YesCaptcha)的集成。
- **HTTP/2 支持**: 许多高级反爬系统通过检测是否使用 HTTP/2 来识别爬虫。

### 5. 数据流与工程化 (Pipeline & Scale)
- **增量抓取 (Incremental Scraping)**: 基于 URL 哈希或 Last-Modified 头,只下载更新过的页面。
- **数据清洗 (Cleansing)**: 使用正则表达式和正则表达式去除 HTML 标签、转义字符及冗余空格。
- **结构化输出**: 自动映射提取结果至 JSON, CSV, 或 SQL 数据库中。
- **并行与分布式**: 利用多线程 (Threading) 或 Scrapy + Redis 实现千万级数据抓取。

## 常用命令/模板

### Playwright Python 基础模板
```python
from playwright.sync_api import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=True)
    context = browser.new_context(user_agent="Mozilla/5.0 ...")
    page = context.new_page()
    page.goto("https://example.com")
    
    # 模拟滚动到底部触发加载
    page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
    page.wait_for_selector(".item-list")
    
    items = page.query_selector_all(".item-list .title")
    results = [item.inner_text() for item in items]
    print(results)
    
    browser.close()

with sync_playwright() as playwright:
    run(playwright)
```

### BeautifulSoup 解析示例
```python
from bs4 import BeautifulSoup
import requests

response = requests.get(url, headers=my_headers)
soup = BeautifulSoup(response.text, 'lxml')

# 使用 CSS Selector
price = soup.select_one('.product-price').get_text(strip=True)
# 使用 XPath (需配合 lxml.etree)
# tree.xpath('//div[@id="title"]/h1/text()')
```

## 边界与限制
- **法律合规性 (Legal & Ethical)**: 严格遵守网站的 `robots.txt`。禁止抓取非公开个人信息。遵循 GDPR 和反不正当竞争法。
- **资源消耗**: 开启浏览器渲染会消耗极大的 CPU 和内存。
- **高频更新**: 目标网站的前端结构一旦变更,爬虫代码必须同步重构。
- **物理障碍**: 强力的验证码(如 HCaptcha/Cloudflare 5s check)有时难以通过程序完美突破。
- **业务干扰**: 禁止进行高频率、破坏性的抓取行为(DDoS 级访问),以免对目标站点造成负担。

---
*注:本技能适用于合规、合理的网页公开数据采集场景。*
* lines: 115
* word count: ~1300 characters

More from seaworld008/Commonly-used-high-value-skills

SkillDescription
agent-hubMulti-agent collaboration plugin that spawns N parallel subagents competing on the same task via git worktree isolation. Agents work independently, results are evaluated by metric or LLM judge, and the best branch is merged. Use when: user wants multiple approaches tried in parallel — code optimization, content variation, research exploration, or any task that benefits from parallel competition. Requires: a git repo.
andrej-karpathy-skillsKarpathy-inspired coding discipline for AI agents: think before coding, keep changes simple, edit surgically, and define verifiable success criteria before implementation.
arenaSpecialist orchestrating codex exec / Antigravity CLI through dual paradigms — COMPETE (multi-variant comparison, select best) and COLLABORATE (decompose tasks across engines, integrate). Supports Solo/Team/Quick execution modes.
azure-kubernetesPlan, create, and configure production-ready Azure Kubernetes Service (AKS) clusters. Covers Day-0 checklist, SKU selection (Automatic vs Standard), networking options (private API server, Azure CNI Overlay, egress configuration), security, and operations (autoscaling, upgrade strategy, cost analysis). WHEN: create AKS environment, provision AKS, enable AKS observability, design AKS networking, choose AKS SKU, secure AKS, optimize AKS, AKS spot nodes, AKS cluster-autoscaler, rightsize AKS pod, pod rightsizing, over-provisioned AKS pod, pod resource requests and limits, Vertical Pod Autoscaler, VPA recommendations.
azure-kubernetes-automatic-readinessAssess Kubernetes workloads and cluster configuration for AKS Automatic compatibility. Identifies incompatibilities, generates fixes, and guides migration from AKS Standard to AKS Automatic. WHEN: migrate to AKS Automatic, check AKS Automatic readiness, validate manifests for Automatic, assess cluster for Automatic compatibility, fix deployment for Automatic compatibility, identify AKS Automatic migration blockers, is my cluster ready for AKS Automatic.
cc-devops-skillsSRE, DevOps, Kubernetes, CI/CD, PromQL, Terraform, Docker, and incident operations playbook for building reliable delivery and operations workflows.
chatgpt-appsBuild, scaffold, refactor, and troubleshoot ChatGPT Apps SDK applications that combine an MCP server and widget UI. Use when Codex needs to design tools, register UI resources, wire the MCP Apps bridge or ChatGPT compatibility APIs, apply Apps SDK metadata or CSP or domain settings, or produce a docs-aligned project scaffold. Prefer a docs-first workflow by invoking the openai-docs skill or OpenAI developer docs MCP tools before generating code.
clayAI 3D model generation agent. Generates text-to-3D and image-to-3D code (Python/JS/OpenSCAD) using Meshy, Tripo, Hunyuan3D, Rodin, Sloyd, and Stability APIs. Handles game pipeline integration, LOD, retopology, UV, and QC validation.
codeql-security-scanner用于通过 CodeQL 执行语义代码扫描、安全查询、自定义规则、SARIF 报告和 GitHub Code Scanning 集成。
complyRegulatory compliance and audit agent. Maps business regulatory requirements (SOC2/PCI-DSS/HIPAA/ISO 27001), checks control implementations, designs audit trails, and implements Policy as Code. Use when compliance auditing is needed.