
Crawl4AI完整指南4步把网页变成LLM可用的Markdown数据【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4aiCrawl4AI 是一个面向 LLM 场景的开源异步网页爬虫与抓取器它把带 JavaScript 渲染、懒加载、动态内容的网页直接转成干净的结构化 Markdown并支持进一步提取结构化 JSON 数据。如果你正在给 RAG 知识库、AI Agent 或数据管道准备网页语料这个工具能把抓网页和喂模型之间最脏的清洗环节一次性解决掉。Crawl4AI 是什么一句话定位网页进Markdown 出。和 Requests BeautifulSoup 这类传统做法相比它做了三件不一样的事内置 Playwright 驱动的浏览器池异步并发抓取动态页面也能拿到渲染后的完整 DOM输出不止 HTML 文本而是带标题层级、表格、链接引用的 Markdown可直接进 LLM 上下文所有环节内容清洗、等待策略、数据提取都是可插拔的 strategy 对象不用改框架代码核心实现在 crawl4ai/ 目录入口类是AsyncWebCrawler想查行为细节可以从 crawl4ai/async_webcrawler.py 入手。安装配置只需3个命令先在虚拟环境里装包然后跑一次初始化自动装浏览器依赖最后用 doctor 体检# 1. 安装 Python 包 pip install -U crawl4ai # 2. 初始化装浏览器及系统依赖 crawl4ai-setup # 3. 验证环境是否正常 crawl4ai-doctor如果 setup 之后浏览器仍有问题手动补一次 Chromiumpython -m playwright install --with-deps chromium装完还可以用 CLI 快速验证不用写 Pythoncrwl https://news.ycombinator.com -o markdown直接输出页面 Markdown。第一个Crawl4AI爬取10行Python代码下面的例子抓取 Hacker News 首页打印清洗后的 Markdown 前 300 字符。注意fit_markdown是去过噪的版本适合喂模型raw_markdown保留更多原始信息。import asyncio from crawl4ai import AsyncWebCrawler async def fetch_front_page(): async with AsyncWebCrawler() as crawler: result await crawler.arun(urlhttps://news.ycombinator.com) if result.success: print(result.markdown.fit_markdown[:300]) asyncio.run(fetch_front_page())运行后你会看到标题、正文被整理成规整的 Markdown 段落导航栏和页脚噪声已自动剔除。动态页面怎么处理JS 触发与懒加载很多列表页的正文是点一下才加载的。Crawl4AI 的解法是把页面交互参数交给CrawlerRunConfigjs_code_before_wait在等待条件之前执行 JS比如先触发加载更多wait_for等某个 CSS 选择器出现确认内容到位再抓取scan_full_page自动滚动整页让懒加载图片与内容全部触发from crawl4ai import AsyncWebCrawler, CrawlerRunConfig config CrawlerRunConfig( js_code_before_wait[ document.querySelector(.load-more)?.click() ], wait_forarticle[data-loaded], # 等首篇内容渲染完成 wait_for_timeout10000, scan_full_pageTrue, # 滚动触发懒加载 scroll_delay0.5, ) async with AsyncWebCrawler() as crawler: result await crawler.arun(urlhttps://example-blog.com/posts, configconfig)结构化数据怎么提取不依赖LLM的两条路拿到 Markdown 后如果还想把价格、标题、规格这类字段抽成 JSON有两类免 LLM 的手段。路线一余弦聚类。CosineStrategy会对页面文本块做语义聚类按你给的关键词挑出最相关的几段不花任何 token 费用from crawl4ai import AsyncWebCrawler, CosineStrategy # 围绕关键词 price 找出页面中最相关的 3 段文本 strategy CosineStrategy(semantic_filterprice, top_k3) async with AsyncWebCrawler() as crawler: result await crawler.arun( urlhttps://example-shop.com/smart-watch, extraction_strategystrategy, ) print(result.extracted_content) # JSON 字符串包含聚类出的文本块路线二CSS 定向选取。页面结构稳定时直接用选择器锁定区域速度最快、结果最确定config CrawlerRunConfig( target_elements[article.product-card], # 只处理商品卡片区域 exclude_external_linksTrue, )如果数据格式复杂到上面两条路都兜不住再上LLMExtractionStrategy配合 JSON Schema让模型按你的结构吐字段。会话保持与批量并发抓取需要登录态、或要连续访问多个同域页面时给CrawlerRunConfig传同一个session_id浏览器上下文会复用Cookie 和页面状态不会丢from crawl4ai import AsyncWebCrawler, CrawlerRunConfig async with AsyncWebCrawler() as crawler: session CrawlerRunConfig(session_idreader) await crawler.arun(urlhttps://example.com/login, configsession) await crawler.arun(urlhttps://example.com/account, configsession) # 复用登录态批量抓多个 URL 则用arun_many它内部走浏览器池并发并带随机间隔模拟人类节奏urls [https://example.com/page1, https://example.com/page2, https://example.com/page3] config CrawlerRunConfig(semaphore_count5, mean_delay0.3, max_range0.5) results await crawler.arun_many(urls, configconfig) ok [r for r in results if r.success] print(f成功 {len(ok)} / {len(urls)})排坑指南上生产前知道的5件事永远先判断result.success。抓取失败超时、DNS、被拦截不会抛异常而是返回successFalse和error_message把它落日志再决定重试。超时参数是page_timeout单位毫秒。新手常写timeout30那不是合法参数慢站建议CrawlerRunConfig(page_timeout60000)善用缓存省钱。默认开启本地缓存重复抓取的 URL 建议加check_cache_freshnessTrue框架会先比 ETag/Last-Modified内容没变就不启动浏览器。反弹窗干扰。大量站点的 Cookie 同意框会挡内容开remove_consent_popupsTrue自动清理遇到未知弹窗则用remove_overlay_elementsTrue。调试时开截图。screenshotTrue会在结果里附上页面截图判断是没渲染出来还是选择器写错一眼就清楚。小结Crawl4AI 的核心价值在于把异步浏览器抓取 Markdown 清洗 结构化提取打包成一套可插拔的 API你只需要在arun/arun_many里声明配置剩下的交给框架。下一步建议直接读 快速入门文档然后按你的真实站点把等待策略和提取策略调到位。【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4ai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考