正在打开工具详情…
正在打开工具详情…
把任意网页抓成干净、适合喂给 LLM 的 Markdown,支持深度爬取、CSS/XPath 结构化抽取和 stealth 反爬。适合做 RAG 数据源与 Agent 联网;代价是自己跑就得自己养浏览器和代理。
要给 RAG 或 Agent 建网页数据源的人:批量抓文档站、竞品页、新闻,需要干净的 Markdown 或结构化 JSON。
只偶尔抓一两个页面的小需求——浏览器与依赖的安装成本不划算;也不适合指望零配置就能稳定突破强反爬站点的人。
Crawl4AI 做的是一件事:把网页变成 LLM 读得顺的 Markdown。它和 requests + 解析库手刮的差别在于自带异步浏览器池与缓存,能跑 JS、等元素、整页滚动再抓,还能出 fit_markdown——用 PruningContentFilterLXML、BM25ContentFilter 或 LLMContentFilter 去掉菜单页脚这类噪音。抽取给了三条路:不用 LLM 的 JsonCssExtractionStrategy / JsonXPathExtractionStrategy / RegexExtractionStrategy,schema 可用 generate_schema 生成;要语义就上 LLMExtractionStrategy,接任意 provider(LiteLLM 支持的都行,包括本地 ollama)。
三条用法要分清:Python 库(免费,浏览器在你自己的 Python 进程里)、自建 Docker 服务端(免费,浏览器在容器里,每个端点都要 CRAWL4AI_API_TOKEN)、Crawl4AI Cloud(按量付费,浏览器、代理、JS 重页面和 bot wall 全由对方处理,另多出只有云端有的 /search、/serp、/answer)。取舍就一句话——JS 重页面和反爬谁来扛:自己跑,代理轮换、enable_stealth、undetected-browser 适配器都归你配。
两个必踩的安装坑:pip install -U crawl4ai 之后必须跑一次 crawl4ai-setup 装浏览器,失败就手动 python -m playwright install --with-deps chromium;起 Docker 服务端必须设 token,否则只有容器内可访问。
上手步骤之外的具体做法:常见场景怎么配、命令怎么写、报错怎么处理。
按第一次用会卡在哪讲:库 / 自建服务端 / 云三种形态怎么选、浏览器必须单独装这件事、不用 LLM 也能结构化抽取、以及深爬与自适应爬取的用法。
| Python 库 | 自建 Docker 服务端 | Crawl4AI Cloud | |
|---|---|---|---|
| 谁跑浏览器 | 你的 Python 进程 | 你的 Docker | 对方 |
| JS 重页面 / 反爬 | 你自己配代理与设置 | 你自己配 | 自动处理 |
| 网页搜索 | 无 | 无 | /search、/serp、/answer |
| 费用 | 免费 | 免费(自付机器) | 按量,有免费额度 |
判断点只有一句:JS 重页面和 bot wall 谁来扛。自己扛就自己配代理轮换与 enable_stealth,也就要承担被封和调试的成本。
这是最常见的「装了但跑不起来」:
bashpip install -U crawl4ai
crawl4ai-setup # 安装并配置浏览器,只需一次
crawl4ai-doctor # 自检失败就手动装:
bashpython -m playwright install --with-deps chromiumpythonimport 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())想要干净正文,上 content filter:
pythonfrom crawl4ai import CrawlerRunConfig, CacheMode
from crawl4ai.content_filter_strategy import PruningContentFilterLXML
from crawl4ai.markdown_generation_strategy import DefaultMarkdownGenerator
run_config = CrawlerRunConfig(
cache_mode=CacheMode.BYPASS,
markdown_generator=DefaultMarkdownGenerator(
content_filter=PruningContentFilterLXML(threshold=0.48, threshold_type="fixed", min_word_threshold=0)
),
)比较 result.markdown.raw_markdown 与 result.markdown.fit_markdown 的长度,就知道滤掉了多少。
列表页、商品页这种结构稳定的,用 CSS schema 更快也更省:
pythonschema = {
"name": "Quotes",
"baseSelector": "div.quote",
"fields": [
{"name": "text", "selector": "span.text", "type": "text"},
{"name": "author", "selector": "small.author", "type": "text"},
],
}
run_config = CrawlerRunConfig(
extraction_strategy=JsonCssExtractionStrategy(schema),
scan_full_page=True, # 滚到底,懒加载的内容才会出现
scroll_delay=0.5,
)懒加载页面一定要开 scan_full_page,否则只拿到首屏。需要语义字段时才用 LLMExtractionStrategy,配 LLMConfig(provider="openai/gpt-4o-mini", api_token=...) 加 Pydantic schema。
bashcrwl https://docs.crawl4ai.com --deep-crawl bfs --max-pages 10BFS / DFS / best-first 三种策略,长任务有 resume_state 断点恢复。不知道该抓多少页时用 AdaptiveCrawler,它对着你给的 query 判断「够了」就停。