by smallnest
Crawl4AI 支持使用 JavaScript、基于模式的提取和灵活的输出格式进行高效的网页抓取。用户可以在无需 LLM 调用的情况下提取数据以实现经济高效的自动化,或使用 LLM 驱动的提取来处理复杂内容。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 crawl4ai 技能" 开始使用
=== crawl4ai 技能 === 作者: smallnest 描述: Crawl4AI 支持使用 JavaScript、基于模式的提取和灵活的输出格式进行高效的网页抓取。用户可以在无需 LLM 调用的情况下提取数据以实现经济高效的自动化,或使用 LLM 驱动的提取来处理复杂内容。 使用方法: 1. 调用技能: "使用 crawl4ai 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 crawl4ai 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
data
low
强大的网页爬取和数据提取技能,支持 JavaScript 渲染、结构化数据提取和多 URL 批量处理。
基于 crawl4ai-skill 代码做基础实现。
# 安装 crawl4ai
pip install crawl4ai
# 安装 Playwright 浏览器
crawl4ai-setup
# 验证安装
crawl4ai-doctor
# 基础爬取,输出 Markdown
crwl https://example.com
# JSON 格式输出
crwl https://example.com -o json
# 绕过缓存,详细输出
crwl https://example.com -o json -v --bypass-cache
import asyncio
from crawl4ai import AsyncWebCrawler
async def main():
async with AsyncWebCrawler() as crawler:
result = await crawler.arun("https://example.com")
print(result.markdown[:500])
asyncio.run(main())
# 搜索并提取前 20 个结果
python scripts/google_search.py "搜索关键词" 20
# 示例
python scripts/google_search.py "2026年Go语言展望" 20
输出格式:
{
"query": "搜索关键词",
"total_results": 20,
"results": [
{
"title": "结果标题",
"link": "https://example.com",
"description": "结果描述",
"site_name": "网站名称"
}
]
}
# 生成提取 schema
python scripts/extraction_pipeline.py --generate-schema https://shop.com "提取所有商品信息"
# 使用 schema 进行提取
crwl https://shop.com -e extract_css.yml -s schema.json -o json
Schema 格式:
{
"name": "products",
"baseSelector": ".product-card",
"fields": [
{"name": "title", "selector": "h2", "type": "text"},
{"name": "price", "selector": ".price", "type": "text"},
{"name": "link", "selector": "a", "type": "attribute", "attribute": "href"}
]
}
# extract_llm.yml
type: "llm"
provider: "openai/gpt-4o-mini"
instruction: "提取商品名称和价格"
api_token: "your-api-token"
crwl https://shop.com -e extract_llm.yml -o json
# 基础 Markdown
crwl https://docs.example.com -o markdown > docs.md
# 过滤后的 Markdown(移除噪音)
crwl https://docs.example.com -o markdown-fit
# 使用 BM25 内容过滤
crwl https://docs.example.com -f filter_bm25.yml -o markdown-fit
过滤器配置:
# filter_bm25.yml
type: "bm25"
query: "机器学习教程"
threshold: 1.0
# 等待特定元素加载
crwl https://example.com -c "wait_for=css:.ajax-content,page_timeout=60000"
# 扫描整个页面
crwl https://example.com -c "scan_full_page=true,delay_before_return_html=2.0"
# Python SDK 并发处理
urls = [
"https://site1.com",
"https://site2.com",
"https://site3.com"
]
results = await crawler.arun_many(urls, config=config)
# login_crawler.yml
session_id: "user_session"
js_code: |
document.querySelector('#username').value = 'user';
document.querySelector('#password').value = 'pass';
document.querySelector('#submit').click();
wait_for: "css:.dashboard"
# 先登录
crwl https://site.com/login -C login_crawler.yml
# 访问受保护内容
crwl https://site.com/protected -c "session_id=user_session"
crawl4ai/
├── README.md # 本文件
├── SKILL.md # 技能详细文档
├── scripts/ # 实用脚本
│ ├── google_search.py # Google 搜索爬虫
│ ├── extraction_pipeline.py # 数据提取管道
│ ├── basic_crawler.py # 基础爬虫
│ └── batch_crawler.py # 批量爬虫
├── references/ # 参考文档
│ ├── cli-guide.md # CLI 完整指南
│ ├── sdk-guide.md # SDK 快速参考
│ └── complete-sdk-reference.md # 完整 API 文档
└── tests/ # 测试文件
├── README.md
├── run_all_tests.py
├── test_basic_crawling.py
├── test_data_extraction.py
├── test_markdown_generation.py
└── test_advanced_patterns.py
| 脚本 | 功能 |
|---|---|
google_search.py | Google 搜索结果爬取,JSON 输出 |
extraction_pipeline.py | 三种提取策略:CSS/LLM/手动 |
basic_crawler.py | 基础网页爬取,带截图功能 |
batch_crawler.py | 批量 URL 处理 |
| 参数 | 说明 | 默认值 |
|---|---|---|
headless | 无头模式 | true |
viewport_width | 视口宽度 | 1920 |
viewport_height | 视口高度 | 1080 |
user_agent | 用户代理 | 随机 |
proxy_config | 代理配置 | null |
| 参数 | 说明 | 默认值 |
|---|---|---|
page_timeout | 页面超时(ms) | 30000 |
wait_for | 等待条件 | null |
cache_mode | 缓存模式 | enabled |
js_code | 执行的 JS | null |
css_selector | CSS 选择器 | null |
--bypass-cachecrwl https://example.com -c "wait_for=css:.dynamic-content,page_timeout=60000"
# browser.yml
headless: false
user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
user_agent_mode: "random"
# 调试模式查看完整输出
crwl https://example.com -o all -v
# 尝试不同的等待策略
crwl https://example.com -c "wait_for=js:document.querySelector('.content')!==null"
MIT License
View Count
0
Download Count
0
Favorite Count
0
Quality Score
84