GitHub 63K Star · 开源 AI 爬虫 · 一行代码网页变数据
⭐ 开源免费 · PythonCrawl4AI 是 GitHub 上最受欢迎的开源 AI 爬虫工具,拥有 63K+ Star。它能将任意网页转换为干净的、LLM 友好的 Markdown 格式,专为 RAG 管道、AI Agent 和数据处理流水线设计。基于 Playwright 驱动浏览器,支持异步并发、反爬绕过、结构化数据提取等高级功能,一行代码即可完成传统爬虫几百行代码的工作。
使用 pip 安装最新版本:
pip install -U crawl4ai
安装浏览器驱动和必要依赖:
crawl4ai-setup
python -m playwright install chromium运行诊断命令确认一切正常:
crawl4ai-doctor
最简单的用法,一行代码爬取网页并获取干净的 Markdown:
import asyncio
from crawl4ai import AsyncWebCrawler
async def main():
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url="https://example.com")
print(result.markdown)
asyncio.run(main())
使用 CSS 选择器从网页提取结构化 JSON 数据,零 LLM 成本:
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig
from crawl4ai import JsonCssExtractionStrategy
schema = {
"name": "商品列表",
"baseSelector": ".product-item",
"fields": [
{"name": "title", "selector": "h3", "type": "text"},
{"name": "price", "selector": ".price", "type": "text"},
{"name": "image", "selector": "img", "type": "attribute", "attribute": "src"}
]
}
async def main():
config = CrawlerRunConfig(
extraction_strategy=JsonCssExtractionStrategy(schema)
)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url="https://example.com/products", config=config)
print(result.extracted_content)
asyncio.run(main())
不写代码也能用,直接命令行爬取:
# 基础爬取输出 Markdown
crwl https://example.com -o markdown
# 深度爬取(BFS 策略,最多 10 页)
crwl https://docs.crawl4ai.com --deep-crawl bfs --max-pages 10
# 用 LLM 提取特定信息
crwl https://example.com/products -q "提取所有商品价格"
一行命令启动服务,自带监控面板和 API 接口:
# 拉取并启动
docker pull unclecode/crawl4ai:latest
docker run -d -p 11235:11235 --name crawl4ai --shm-size=1g unclecode/crawl4ai:latest
# 访问监控面板
# http://localhost:11235/dashboard
# 访问交互式测试页
# http://localhost:11235/playground
运行 python -m playwright install chromium 手动安装浏览器。如果仍有问题,尝试 playwright install --with-deps chromium。
Crawl4AI v0.8.5+ 内置三级反爬检测和代理轮换。配置 proxy_config 参数添加代理链,系统会自动升级反爬策略。
通过 LiteLLM 集成,支持 OpenAI、Claude、Gemini、本地 Ollama 等所有主流模型。也支持不依赖 LLM 的 CSS/XPath 提取。
支持!使用 Browser Profiler 创建持久化的浏览器配置文件,保存登录状态、Cookie 等,后续爬取自动复用。