机器翻译正文由机器翻译自项目原始文档(英文),排版经程序统一处理,可能存在偏差,请以原项目仓库为准。
🚀🤖 Crawl4AI:面向大语言模型和 AI 代理的开源网络爬虫
Crawl4AI 将任何网站转换为干净的、适用于 RAG、AI 代理和数据管道的 Markdown。自己运行开源网页爬虫和抓取工具,永久免费,或使用托管版本,只需一个密钥即可:通过一个 API 进行抓取、搜索和提取,并为您的代理提供 MCP。
使用 Crawl4AI 的两种方式
🐍 自己运行:开源,永久免费
pip install -U crawl4ai
crawl4ai-setup # installs the browser, once
import asyncio
from crawl4ai import AsyncWebCrawler
async def main():
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url="https://news.ycombinator.com")
print(result.markdown)
asyncio.run(main())
Docker 服务器、CLI 和所有选项:安装 · docs.crawl4ai.com
☁️ 或使用云端:无需浏览器,无需代理
- 验证您的电子邮件,您的第一个 $10 套装由我们提供(截至 2026 年 12 月 31 日,然后起步价为 $5)。无需信用卡。
-
将任何页面获取为 Markdown:
curl -s https://api.crawl4ai.com/scrape \ -H "Authorization: Bearer $CRAWL4AI_KEY" \ -H "Content-Type: application/json" \ -d '{"url": "https://news.ycombinator.com"}' | jq -r .markdown同一个密钥可用于
/search、/answer、/extract以及多个 URL(/scrape/batch、/scrape/jobs)。按使用付费:实时价格。 -
将它交给你的 AI 代理。显示 Claude 代码;Codex、Cursor 和 OpenCode →
claude mcp add --transport http crawl4ai https://api.crawl4ai.com/mcp \ --header "Authorization: Bearer $CRAWL4AI_KEY"
哪一个?
| 🐍 库 | 🐳 你自己的服务器 | ☁️ Crawl4AI 云 | |
|---|---|---|---|
| 运行浏览器 | 你,在你的 Python 进程中 | 你,在你机器上的 Docker 中 | 我们来做 |
| JS-heavy 页面和机器人防护 | 你的设置,你的代理 | 你的设置,你的代理 | 自动为你处理 |
| 网络搜索 | – | – | /search 和 /answer |
| 价格 | 永久免费 | 免费(你的托管) | 按使用付费;你的前 $10 由我们承担 |
🤓 我的个人故事
我在 Amstrad 上长大,多亏了我爸爸,从未停止过构建。在研究生阶段,我专攻 NLP,并为研究构建爬虫。这是我了解到提取数据的重要性的地方。
在 2023 年,我需要 web 到 Markdown 的工具。“开源”选项需要账户、API token 和 $16,结果还不尽如人意。我怒气冲天,在几天内构建了 Crawl4AI,它很快就流行起来。现在它是 GitHub 上星数最多的爬虫。
我将其开源,以便 可用性 ,任何人都可以无门槛使用。现在我正在构建平台以便 负担得起 ,任何人都可以在不破坏预算的情况下进行认真的爬取。如果你认同这个理念,加入我们,发送反馈,或者仅仅抓取一些令人惊叹的内容。
该平台现已上线:Crawl4AI 云。
为什么开发者选择 Crawl4AI
- LLM准备输出:带有标题、表格、代码和引用提示的智能Markdown
- 实践中快速:异步浏览器池、缓存、最少跳转
- 完全控制:会话、代理、Cookies、用户脚本、钩子
- 自适应智能:学习网站模式,只探索重要内容
- 随处部署:无需密钥,CLI 和 Docker,或托管云
✨ 功能
📝 Markdown 生成
- 🧹 清理 Markdown:标题、列表、表格和代码块,以 LLM 能够良好读取的结构。
- 🎯 适配 Markdown:过滤器移除菜单、页脚和模板内容:
PruningContentFilterLXML,BM25ContentFilter(针对查询)以及LLMContentFilter。 - 🔗 引用:页面链接变为编号参考列表。
- 🛠️ 自定义策略:插入自定义 Markdown 生成器。
☁️ 云端同样适用:POST /scrape 返回这个 Markdown,没有浏览器来运行。文档 →
📊 结构化数据提取
- 🔎 CSS 和 XPath 模式:无需 LLM 的快速抽取(
JsonCssExtractionStrategy,JsonXPathExtractionStrategy,RegexExtractionStrategy)。 - 🪄 模式生成器:只需描述一次你想要的内容;
generate_schema会写出可重复使用的模式。 - 🤖 LLM 抽取:任何 LLM 提供商,开源或托管的,都能生成类型化 JSON 模式(
LLMExtractionStrategy)。 - 🧱 分块:针对长页面的主题、正则和句子分块。
- 🌌 余弦相似度:查找与查询匹配的块(
CosineStrategy)。
☁️ 云端同样适用:POST /extract,无需您自己的 LLM 密钥。文档 →
🌐 浏览器控制
- 🖥️ 你自己的浏览器:持久化配置文件,保存登录信息、Cookie 和设置。
- 🔄 远程浏览器:通过 Chrome DevTools 协议 (CDP) 连接。
- 🔒 会话:在多步骤爬取中保持浏览器状态。
- 🧩 代理:支持认证和轮换。
- 🕶️ 隐身模式:
enable_stealth,以及一个未被检测的浏览器适配器,用于检测自动化的网站。 - ⚙️ 完全控制:头信息、Cookie、用户代理、视口。
- 🌍 Chromium、Firefox 和 WebKit。
🔎 爬取和抓取
- 🕸️ 深度爬取:BFS、DFS 和最佳优先策略,对长时间爬取提供崩溃恢复(
resume_state)。 - 🧠 自适应爬取:
AdaptiveCrawler在学习到足够信息以回答您的查询后停止。 - 🌱 URL 发现:
AsyncUrlSeeder(站点地图,Common Crawl)和DomainMapper;prefetch=True可以快 5 到 10 倍找到 URL。 - 🚀 动态页面:运行 JavaScript,等待元素,滚动完整页面(
scan_full_page)以支持无限滚动和懒加载图像。 - 📸 网页截图和 PDF。
- 🖼️ 媒体和链接:图片、音频、视频、
srcset、内部和外部链接、iframe、元数据。 - 📂 原始 HTML 和本地文件:
raw:和file://。 - 🛠️ 每一步爬取的钩子。
- 💾 缓存以跳过重复抓取。
- ⚡ 同时处理多个 URL:
arun_many搭配内存自适应分配器。
☁️ 云端同样适用:一次流式调用最多 50 个 URL,或在后台任务中处理最多 10,000 个 URL。文档 →
🐳 自托管(Docker)
- 🔐 默认安全:每个端点都需要您的
CRAWL4AI_API_TOKEN。 - 🧰 REST API:
/md,/html,/crawl,/crawl/stream,/screenshot,/pdf,/execute_js. - 🤖 MCP:将 Claude Code 和其他代理连接到您自己的服务器。
- 📊 监控仪表板和游乐场,带有预热页面的浏览器池。
- 🏗️ AMD64 和 ARM64 镜像。
☁️ 不想自己运行服务器?云端也是同样的概念,托管的。获取密钥 →
☁️ 云端所增加的内容
🛠️ 安装
🐍 pip
pip 安装 -U crawl4ai
crawl4ai-setup # 安装并设置浏览器
crawl4ai-doctor # 检查安装
如果浏览器设置失败,请手动安装:
python -m playwright install --with-deps chromium
预发布版本: pip install crawl4ai --pre
开发安装,适用于贡献者:
git clone https://github.com/unclecode/crawl4ai.git
cd crawl4ai
pip install -e ".[all]" # 或者: pip install -e . (仅核心)
🐳 Docker 服务器
服务器需要一个令牌。没有令牌,它只会在其容器内部响应。
导出 CRAWL4AI_API_TOKEN="$(openssl rand -hex 32)"
docker run -d -p 11235:11235 --name crawl4ai --shm-size=1g \
-e CRAWL4AI_API_TOKEN="$CRAWL4AI_API_TOKEN" \
unclecode/crawl4ai:latest
测试它(启动大约需要 10 秒):
curl -s http://localhost:11235/md \
-H "Authorization: Bearer $CRAWL4AI_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{"url": "https://news.ycombinator.com"}' | jq -r .markdown
仪表板在 http://localhost:11235/dashboard,操场在 http://localhost:11235/playground。LLM 密钥、MCP 和所有设置:自托管指南。
⌨️ 命令行(crwl)
# 将页面作为 Markdown
crwl https://news.ycombinator.com -o markdown
# 深度爬取,广度优先,最多 10 个页面
crwl https://docs.crawl4ai.com --deep-crawl bfs --max-pages 10
# 对页面提问(需要 LLM 密钥:crwl 配置)
crwl https://www.example.com/products -q "提取所有产品价格"
🔬 高级使用示例
更多内容请见 docs/examples。
📝 清理并适配 Markdown
import asyncio
从 crawl4ai 导入 AsyncWebCrawler、BrowserConfig、CrawlerRunConfig、CacheMode 从 crawl4ai.content_filter_strategy 导入 PruningContentFilterLXML 从 crawl4ai.markdown_generation_strategy 导入 DefaultMarkdownGenerator
async def main():
run_config = CrawlerRunConfig(
cache_mode=CacheMode.BYPASS,
markdown_generator=DefaultMarkdownGenerator(
content_filter=PruningContentFilterLXML(threshold=0.48, threshold_type="fixed", min_word_threshold=0)
),
)
async with AsyncWebCrawler(config=BrowserConfig(headless=True)) as crawler:
result = await crawler.arun(url="https://en.wikipedia.org/wiki/Web_crawler", config=run_config)
print(len(result.markdown.raw_markdown), "characters of raw Markdown")
print(len(result.markdown.fit_markdown), "characters after the filter")
asyncio.run(main())
🖥️ 一个 JavaScript 页面和结构化数据,无需 LLM
import asyncio, json
从 crawl4ai 导入 AsyncWebCrawler、BrowserConfig、CrawlerRunConfig、CacheMode、JsonCssExtractionStrategy
schema = {
"name": "Quotes",
"baseSelector": "div.quote",
"fields": [
{"name": "text", "selector": "span.text", "type": "text"},
{"name": "author", "selector": "small.author", "type": "text"},
{"name": "tags", "selector": "a.tag", "type": "list", "fields": [{"name": "tag", "type": "text"}]},
],
}
异步 def main():
run_config = CrawlerRunConfig(
extraction_strategy=JsonCssExtractionStrategy(schema),
scan_full_page=True, # scroll to the end, so the page loads every quote
scroll_delay=0.5,
cache_mode=CacheMode.BYPASS,
)
async with AsyncWebCrawler(config=BrowserConfig(headless=True)) as crawler:
result = await crawler.arun(url="https://quotes.toscrape.com/scroll", config=run_config)
quotes = json.loads(result.extracted_content)
print(f"Extracted {len(quotes)} quotes")
print(json.dumps(quotes[0], indent=2))
asyncio.run(main())
📚 使用LLM的结构化数据
import os, asyncio
from pydantic import BaseModel, Field
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig, CacheMode, LLMConfig, LLMExtractionStrategy
class ModelFee(BaseModel):
model_name: str = Field(..., description="Name of the model.")
input_fee: str = Field(..., description="Fee for input tokens.")
output_fee: str = Field(..., description="Fee for output tokens.")
异步 def main():
run_config = CrawlerRunConfig(
cache_mode=CacheMode.BYPASS,
extraction_strategy=LLMExtractionStrategy(
# any provider LiteLLM supports, e.g. "ollama/llama3.3" with api_token="no-token"
llm_config=LLMConfig(provider="openai/gpt-4o-mini", api_token=os.getenv("OPENAI_API_KEY")),
schema=ModelFee.model_json_schema(),
extraction_type="schema",
instruction="Extract every model name with its input and output token fee.",
),
)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url="https://openai.com/api/pricing/", config=run_config)
print(result.extracted_content)
asyncio.run(main())
🤖 你自己的带有已保存配置文件的浏览器
import os, asyncio
from pathlib import Path 从 crawl4ai 导入 AsyncWebCrawler, BrowserConfig, CrawlerRunConfig, CacheMode
async def main():
user_data_dir = os.path.join(Path.home(), ".crawl4ai", "browser_profile")
os.makedirs(user_data_dir, exist_ok=True)
browser_config = BrowserConfig(headless=True, user_data_dir=user_data_dir, use_persistent_context=True)
run_config = CrawlerRunConfig(cache_mode=CacheMode.BYPASS, magic=True)
async with AsyncWebCrawler(config=browser_config) as crawler:
result = await crawler.arun(url="ADDRESS_OF_A_CHALLENGING_WEBSITE", config=run_config)
print(result.success, len(result.markdown))
asyncio.run(main())
📖 文档
- 库文档、指南和 API 参考:docs.crawl4ai.com
- 云文档:crawl4ai.com/docs
- 发布说明:releases · 路线图:ROADMAP.md
🤝 贡献
我们欢迎开源社区的贡献。请查看我们的贡献指南以获取更多信息。
📄 许可与归属
本项目根据 Apache 2.0 许可协议授权,建议通过以下徽章进行鸣谢。详情请参阅 Apache 2.0 许可协议 文件。
鸣谢要求
使用 Crawl4AI 时,您必须包含以下其中一种鸣谢方式:
📈 1. 徽章鸣谢(推荐)
将以下徽章之一添加到您的 README、文档或网站中:
| 主题 | 徽章 |
|---|---|
| 迪斯科主题(动画) | |
| 夜间主题(暗色带霓虹) | |
| 深色主题(经典) | |
| 浅色主题(经典) |
添加徽章的HTML代码:
<!-- 迪斯科主题(动画效果) -->
<a href="https://github.com/unclecode/crawl4ai">
</a>
<!-- 夜间主题(暗色带霓虹) -->
<a href="https://github.com/unclecode/crawl4ai">
</a>
<!-- 黑暗主题(经典) -->
<a href="https://github.com/unclecode/crawl4ai">
</a>
<!-- 明亮主题(经典) -->
<a href="https://github.com/unclecode/crawl4ai">
</a>
<!-- 简易盾徽章 -->
<a href="https://github.com/unclecode/crawl4ai">
</a>
📖 2. 文本归属
将此行添加到您的文档中:
本项目使用 Crawl4AI (https://github.com/unclecode/crawl4ai) 进行网页数据提取。
📚 引用
如果您在研究或项目中使用 Crawl4AI,请引用:
@software{crawl4ai2024,
author = {UncleCode},
title = {Crawl4AI: Open-source LLM Friendly Web Crawler & Scraper},
year = {2024},
publisher = {GitHub},
journal = {GitHub Repository},
howpublished = {\url{https://github.com/unclecode/crawl4ai}},
commit = {Please use the commit hash you're working with}
}
文本引用格式:
UncleCode. (2024). Crawl4AI: Open-source LLM Friendly Web Crawler & Scraper [Computer software].
GitHub. https://github.com/unclecode/crawl4ai
🗾 使命
我们的使命是通过将数字足迹转化为结构化、有用的资产来释放个人和企业数据的价值。Crawl4AI 为个人和组织提供开源工具来提取和结构化数据,并提供公平的方式从中获益。完整使命声明 →
💖 支持 Crawl4AI
- ⭐ 给仓库点星:这能帮助更多人找到它。
- ☁️ 使用云服务:crawl4ai.com。它为库提供资金支持。
- 💝 在 GitHub 赞助:github.com/sponsors/unclecode
- 🏢 公司:赞助层级和福利见 SPONSORS.md。
📧 联系
Discord · X @unclecode · GitHub @unclecode · hello@crawl4ai.com
- 以创建爬虫或人工智能代理为生? 在 X 上给我发私信。我想和你们这样的人合作,而且我们正在招聘。
- 来自公司吗? 我们有企业方案,并可根据您的业务进行定制。SOC 2 类型 I 已完成,类型 II 正在进行中。请写信至 hello@crawl4ai.com.
爬行愉快!🕸️🚀
收藏历史
- 本文标题:crawl4ai - 面向大型语言模型(LLM)和 AI 代理的开源网页爬
- 本文链接:https://www.cn121.com/automation/unclecode-crawl4ai.html
- 原项目:unclecode/crawl4ai 版权归原作者 unclecode 及贡献者所有
- 收录信息:本站于 2026-09-26 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
- 开源协议:收录时本项目采用 Apache-2.0(查看 LICENSE 原文),本站译文为其衍生内容;使用、修改、分发请以该仓库 LICENSE 原文为准。本站对原文仅作排版与图片地址适配, 并保留原项目的 NOTICE 与署名要求。
- 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 unclecode/crawl4ai。
- 翻译说明:本页正文为人工智能生成内容——由机器翻译对原项目 README 初译、经程序校验排版,可能存在错漏,请以原项目文档为准。
- 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
- 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。