ARTICLE DETAIL

资讯详情

深耕商务建站与企业官网运营的一线实战洞察。

Crawl4AI 快速指南:AI 数据抓取的网页爬虫,3 个示例完成 Markdown 转换

Crawl4AI 快速指南:AI 数据抓取的网页爬虫,3 个示例完成 Markdown 转换 Crawl4AI 快速指南AI 数据抓取的网页爬虫3 个示例完成 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/crawl4ai假设你下午 3 点要给 RAG 流水线灌一批网页语料只有 5 分钟准备时间。Crawl4AI 是一个输出对 LLM 友好的 Markdown 的 Python 网页爬虫下面用 3 个示例从安装一路走到反爬处理——把页面转成 Markdown、清洗导航与弹窗、点按加载动态内容最后留一条 crwl 命令方便随时抽查。三行命令装好 Crawl4AI 并跑通浏览器安装一共三行第二行做安装后初始化第三行做环境自检pip install -U crawl4ai crawl4ai-setup crawl4ai-doctor自检提示缺浏览器时用下面这条命令把 Chromium 和系统依赖一次装齐python -m playwright install --with-deps chromium除稳定版外还有三种常用装法用途命令预发布版尝新功能pip install crawl4ai --pre开发模式改源码git clone https://gitcode.com/GitHub_Trending/craw/crawl4ai然后cd crawl4ai pip install -e .全量功能PDF、本地模型、embeddingpip install crawl4ai[all]不用写 Python 也能先验证环境这条命令直接抓一个页面并输出 Markdowncrwl https://en.wikipedia.org/wiki/Apple -o markdown最小 Python 爬虫一次 arun 拿到 Markdown 结果下面这个脚本只做一件事抓一个 URL打印前 500 个字符的 Markdown。import asyncio from crawl4ai import AsyncWebCrawler async def main(): async with AsyncWebCrawler() as crawler: result await crawler.arun(urlhttps://en.wikipedia.org/wiki/Apple) if result.success: print(result.markdown.raw_markdown[:500]) if __name__ __main__: asyncio.run(main())arun返回的result是 CrawlResult常用字段有success是否成功、markdownMarkdownGenerationResult、metadata页面标题、描述等 meta 信息、links与media页面里收集的链接和媒体、redirected_url实际跳转后的地址。raw_markdown 和 fit_markdown 的区别决定喂给 AI 的是哪一份result.markdown.raw_markdown是整页转换结果result.markdown.fit_markdown是精简版去掉导航、页脚之类的噪声只留主体。直接print(result.markdown)等价于打印 raw_markdown。给 RAG 喂数据时一般用 fit_markdown省 token要控制精简力度就去调下一节的过滤器参数。用 excluded_tags 和 remove_overlay_elements 去掉导航与 Cookie 弹窗下面这段配置排除三类标签不转 Markdown并在取 HTML 前先移除 Cookie 弹窗这类覆盖层from crawl4ai import BrowserConfig, CrawlerRunConfig, CacheMode browser_config BrowserConfig( headlessTrue, # 无头模式 java_script_enabledTrue, # 执行页面 JS默认就是 True ) crawler_config CrawlerRunConfig( cache_modeCacheMode.BYPASS, # 本次不读也不写缓存 excluded_tags[nav, footer, aside], remove_overlay_elementsTrue, # 移除弹窗等覆盖元素 page_timeout60000, # 导航超时单位毫秒 ) async with AsyncWebCrawler(configbrowser_config) as crawler: result await crawler.arun( urlhttps://en.wikipedia.org/wiki/Apple, configcrawler_config, )cache_mode共 5 种取值BYPASS本次读写全绕过、ENABLED正常读写、READ_ONLY、WRITE_ONLY、DISABLED完全禁用缓存。调试阶段建议保持 BYPASS避免读到上一次的旧页面。用 PruningContentFilter 的阈值把 Markdown 再压一遍下面给 Markdown 生成器挂上剪枝过滤器对文本块逐段打分低于阈值的丢弃同时跳过链接转换from crawl4ai.markdown_generation_strategy import DefaultMarkdownGenerator from crawl4ai.content_filter_strategy import PruningContentFilter crawler_config CrawlerRunConfig( cache_modeCacheMode.BYPASS, markdown_generatorDefaultMarkdownGenerator( content_filterPruningContentFilter( threshold0.48, # 固定阈值低于此分的块被剪掉 threshold_typefixed, min_word_threshold0, ), options{ignore_links: True}, ), )0.48 是默认阈值如果发现正文也被剪掉了可以降到 0.30.4再对比fit_markdown的长度变化来定档。动态页面抓取js_code 点按加载 延时等渲染对于点按加载更多才出内容的页面注入一段 JS 找到按钮并触发点击再用delay_before_return_html等渲染完成后才取 HTMLcrawler_config CrawlerRunConfig( cache_modeCacheMode.BYPASS, js_code const btn Array.from(document.querySelectorAll(button)) .find(b b.textContent.includes(Load More)); btn btn.click(); , delay_before_return_html2, # 取 HTML 前等 2 秒单位是秒 )用 css_selector 只提取目标区块只要列表正文时css_selector会把转换范围限定到匹配的元素上Markdown 里就只有这些区块的内容crawler_config CrawlerRunConfig( cache_modeCacheMode.BYPASS, css_selector.wide-tease-item__description, )绕过反爬proxy_config 挂代理、user_agent 换指纹、magic 加模拟交互拿到 403 或人机验证页时按顺序试先换 UA再挂代理最后开防检测组合browser_config BrowserConfig( user_agentMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36, user_agent_moderandom, # 设为 random 时自动生成随机 UA proxy_config{ server: http://proxy.example.com:8080, username: user, password: pass, }, ) crawler_config CrawlerRunConfig( magicTrue, # 自动处理弹窗等覆盖层 simulate_userTrue, # 模拟鼠标移动与点击 )⚠️ 这里坑比较多proxy_config是BrowserConfig的参数不是CrawlerRunConfig的写错位置会静默失效。踩坑记录三则浏览器依赖缺失、动态页面不全、被反爬拦截浏览器依赖缺失Chromium 起不来、报缺 so 库时跑python -m playwright install --with-deps chromium再执行crawl4ai-doctor复查。动态页面内容不全通常是渲染没完成就取了 HTML。把delay_before_return_html秒调到 23或指定wait_untilnetworkidle等网络静默。被反爬拦截先上magicTruesimulate_userTrue不行再挂proxy_config并发爬取内存吃紧时BrowserConfig(memory_saving_modeTrue)并关掉截图这类用不到的功能。下一步可以做什么深度爬取BFS/DFS/最佳优先逐页扩展说明见PROGRESSIVE_CRAWLING.md源码在crawl4ai/deep_crawling/目录LLM 结构化提取从 Markdown 里按 schema 抽取字段示例在docs/examples/llm_extraction_openai_pricing.pyDocker REST API 部署服务入口是deploy/docker/server.py部署说明见deploy/docker/README.md【免费下载链接】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),仅供参考
返回列表
PREV
查看更多资讯
NEXT
返回资讯列表