Firecrawl AI 网页抓取
一句话概述
Firecrawl 是一个专为 AI 和 LLM 设计的网页抓取 API,一个 API 调用就能把任何网页转成干净的 Markdown、结构化 JSON 或截图,自动处理 JavaScript 渲染、反爬虫检测和代理轮换,让你专注于构建 AI 应用而不是折腾爬虫。
核心知识点表格
| 知识点 | 说明 |
|---|
| 项目地址 | https://github.com/firecrawl/firecrawl |
| 官网 | https://www.firecrawl.dev |
| GitHub Stars | 110,000+ |
| 核心功能 | 网页 → Markdown/JSON/HTML 转换 |
| API 模式 | Scrape(单页)/ Crawl(全站)/ Map(站点地图)/ Search(搜索)/ Agent(智能提取) |
| SDK 支持 | Python、Node.js、Java、Rust、Go、Elixir、PHP |
| 许可证 | AGPL-3.0 |
| 定价 | 免费版 500 积分/月,付费 $19/月起 |
| 团队背景 | Y Combinator 孵化,累计融资 $16.2M |
安装与配置
方式一:使用云端 API(最简单)
# 第一步:注册账号获取 API Key
# 访问 https://www.firecrawl.dev 注册
# 第二步:安装 Python SDK
pip install firecrawl-py # 安装 Python 客户端
from firecrawl import FirecrawlApp # 导入 Firecrawl
# 初始化客户端
app = FirecrawlApp(api_key="fc-your-api-key") # 使用你的 API Key
方式二:自托管部署(开源版)
# 克隆仓库
git clone https://github.com/firecrawl/firecrawl.git # 下载源码
cd firecrawl # 进入目录
# 复制环境变量文件
cp .env.example .env # 复制配置模板
# 编辑 .env 文件(设置必要的环境变量)
# FIRECRAWL_API_KEY=fc-your-key
# REDIS_URL=redis://redis:6379
# 使用 Docker Compose 启动
docker compose up -d # 后台启动所有服务
# 服务启动后,API 地址为 http://localhost:3002
Node.js SDK 安装
npm install @mendable/firecrawl-js # 安装 Node.js SDK
基本使用
Scrape 模式:抓取单个页面
from firecrawl import FirecrawlApp # 导入库
app = FirecrawlApp(api_key="fc-xxx") # 初始化
# 抓取单个页面,转成 Markdown
result = app.scrape_url(
"https://docs.python.org/3/tutorial/", # 目标 URL
params={
"formats": ["markdown"], # 输出格式:Markdown
}
)
print(result["markdown"]) # 打印 Markdown 内容
# 输出的 Markdown 干净整洁,去掉了导航栏、广告等杂质
Crawl 模式:爬取整个站点
# 从根 URL 开始,递归爬取整个网站的所有页面
crawl_result = app.crawl_url(
"https://docs.python.org/3/", # 起始 URL
params={
"limit": 50, # 最多爬取 50 个页面
"scrapeOptions": {
"formats": ["markdown"] # 每页转成 Markdown
}
},
poll_interval=5 # 每 5 秒检查一次爬取进度
)
# 遍历所有页面
for page in crawl_result["data"]:
print(f"URL: {page['metadata']['url']}") # 页面 URL
print(f"标题: {page['metadata']['title']}") # 页面标题
print(f"内容长度: {len(page['markdown'])} 字符") # 内容长度
print("---")
Map 模式:获取站点地图
# 快速获取网站的所有 URL(不抓取内容)
map_result = app.map_url(
"https://docs.python.org/3/" # 目标网站
)
# 打印发现的所有 URL
for url in map_result["links"][:10]: # 显示前 10 个
print(url)
Search 模式:搜索并提取内容
# 搜索网页并获取完整内容(一步到位)
search_result = app.search(
"宏基因组分析流程 bioinformatics pipeline", # 搜索关键词
params={
"limit": 5 # 返回前 5 条结果
}
)
for result in search_result["data"]:
print(f"标题: {result['metadata']['title']}") # 标题
print(f"内容: {result['markdown'][:200]}...") # 内容前 200 字
print("---")
高级用法
结构化数据提取(JSON Schema)
# 用 JSON Schema 定义你想提取的数据结构
schema = {
"type": "object",
"properties": {
"title": {"type": "string"}, # 文章标题
"author": {"type": "string"}, # 作者
"date": {"type": "string"}, # 发布日期
"summary": {"type": "string"}, # 摘要
"tags": {
"type": "array", # 标签列表
"items": {"type": "string"}
}
}
}
# 抓取页面并按 Schema 提取结构化数据
result = app.scrape_url(
"https://example.com/blog/article",
params={
"formats": ["extract"], # 使用提取模式
"extract": {
"schema": schema # 传入 JSON Schema
}
}
)
print(result["extract"]) # 输出结构化 JSON 数据
# {"title": "...", "author": "...", "date": "...", "summary": "...", "tags": [...]}
批量抓取(Batch Scrape)
# 批量处理多个 URL(异步执行,适合大规模抓取)
urls = [
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3",
]
batch_result = app.batch_scrape_urls(
urls, # URL 列表
params={"formats": ["markdown"]}, # 输出格式
poll_interval=5 # 轮询间隔
)
for page in batch_result["data"]:
print(f"URL: {page['metadata']['url']}")
print(f"内容: {page['markdown'][:100]}...")
带交互操作的抓取(Actions)
# 对于需要点击、滚动等交互的页面
result = app.scrape_url(
"https://example.com/dynamic-page",
params={
"formats": ["markdown"],
"actions": [
{"type": "click", "selector": ".load-more"}, # 点击"加载更多"按钮
{"type": "wait", "milliseconds": 2000}, # 等待 2 秒
{"type": "scroll", "direction": "down"}, # 向下滚动
{"type": "wait", "milliseconds": 1000}, # 再等 1 秒
]
}
)
MCP 集成(让 AI 助手直接调用)
// 在 Claude Desktop 或 Cursor 的 MCP 配置中添加
{
"mcpServers": {
"firecrawl": {
"command": "npx",
"args": ["-y", "firecrawl-mcp"],
"env": {
"FIRECRAWL_API_KEY": "fc-your-api-key"
}
}
}
}
// 配置后,AI 助手可以直接抓取网页获取信息
常见报错与解决
| 报错信息 | 原因 | 解决方法 |
|---|
401 Unauthorized | API Key 无效 | 检查 API Key 是否正确,是否过期 |
402 Payment Required | 免费额度用完 | 升级付费套餐或等待月初额度刷新 |
429 Rate Limit | 请求太频繁 | 降低请求频率,使用 batch 模式 |
Timeout | 目标网页加载太慢 | 增加超时时间,或检查目标网站是否可访问 |
Empty content | 页面内容是动态加载的 | 使用 actions 等待内容加载 |
Blocked by anti-bot | 被目标网站反爬 | 使用云端版(自带代理池),自托管需配置代理 |
速查表
# === Firecrawl Python SDK 速查 ===
from firecrawl import FirecrawlApp
app = FirecrawlApp(api_key="fc-xxx")
# 抓取单页 → Markdown
app.scrape_url("https://...", params={"formats": ["markdown"]})
# 抓取单页 → HTML
app.scrape_url("https://...", params={"formats": ["html"]})
# 抓取单页 → 截图
app.scrape_url("https://...", params={"formats": ["screenshot"]})
# 结构化提取
app.scrape_url("https://...", params={"formats": ["extract"], "extract": {"schema": {...}}})
# 爬取整站
app.crawl_url("https://...", params={"limit": 100})
# 站点地图
app.map_url("https://...")
# 搜索 + 提取
app.search("关键词", params={"limit": 5})
# 批量抓取
app.batch_scrape_urls(["url1", "url2"], params={"formats": ["markdown"]})
与同类工具对比
| 特性 | Firecrawl | Crawl4AI | BeautifulSoup | Selenium |
|---|
| 核心定位 | AI 网页抓取 API | AI 网页抓取 | HTML 解析 | 浏览器自动化 |
| JS 渲染 | 自动处理 | 支持 | 不支持 | 支持 |
| 反爬处理 | 自带代理池 | 基本 | 无 | 需自行配置 |
| 输出格式 | Markdown/JSON/HTML | Markdown | HTML | HTML |
| 上手难度 | 非常简单(一个 API) | 简单 | 中等 | 较难 |
| 开源 | AGPL-3.0 | Apache 2.0 | MIT | Apache 2.0 |
| 云端服务 | 有 | 无 | 无 | 无 |
| 价格 | 免费500积分+付费 | 完全免费 | 完全免费 | 完全免费 |
选择建议:如果你要做 RAG 或 AI Agent 需要高质量网页数据,Firecrawl 是最省心的选择。如果预算有限且能自己管理基础设施,Crawl4AI 是免费替代品。纯 HTML 解析用 BeautifulSoup 足够。