详解AI采集框架Crawl4AI,打造智能网络爬虫

news2025/4/9 14:04:37

大家好,Crawl4AI作为开源Python库,专门用来简化网页爬取和数据提取的工作。它不仅功能强大、灵活,而且全异步的设计让处理速度更快,稳定性更好。无论是构建AI项目还是提升语言模型的性能,Crawl4AI都能帮您简化工作流程。它可以直接在Python项目中使用,或者将其集成到REST API中,实现快速、稳定的数据爬取和处理。这样,无论是数据的实时获取还是后续的分析处理,都能更加得心应手。

1.快速使用

以下是个简单的例子,展示了Crawl4AI强大的异步能力:

import asyncio
from crawl4ai import AsyncWebCrawler

asyncdef main():
    # 初始化异步网页爬虫
    asyncwith AsyncWebCrawler(verbose=True) as crawler:
        # 爬取指定的 URL
        result = await crawler.arun(url="https://www.nbcnews.com/business")
        # 以 Markdown 格式显示提取的内容
        print(result.markdown)

# 执行异步主函数
if __name__ == "__main__":
    asyncio.run(main())

解释:

  • 导入库:从crawl4ai库中导入AsyncWebCrawlerasyncio模块。

  • 创建异步上下文:使用异步上下文管理器实例化AsyncWebCrawler

  • 运行爬虫:使用arun() 法异步爬取指定的 URL 并提取有意义的内容。

  • 打印结果:输出提取的内容,格式化为 Markdown。

  • 执行异步函数:使用asyncio.run()执行异步的main函数。

2.特性亮点

Crawl4AI具备以下核心特性,让网页爬取和数据提取工作更加高效:

  • 开源免费:无额外费用,开源可信赖。

  • 快速性能:速度超越许多付费工具。

  • 多样输出:支持JSON、清洁HTML、Markdown格式。

  • 多URL并发:一次性处理多个网页,提升效率。

  • 媒体提取:全面抓取图片、音频、视频等。

  • 链接全收集:不遗漏任何内外链接。

  • 元数据抽取:深入提取网页信息。

  • 自定义操作:自定义请求头、认证,修改页面后再爬取。

  • 用户代理模拟:模拟不同设备访问。

  • 页面截图:快速获取网页视觉快照。

  • JavaScript支持:执行JS获取动态内容。

  • 数据结构化:精确提取结构化数据。

  • 智能提取技术:使用余弦聚类和LLM技术。

  • CSS选择器:精准定位数据。

  • 指令优化:通过指令提升提取效果。

  • 代理配置:增强访问权限和隐私保护。

  • 会话管理:轻松处理多页爬取。

  • 异步架构:提升性能和可扩展性。

3.安装指南

Crawl4AI提供了多种安装方式,以适应不同的使用场景。以下是几种常用的安装方法:

3.1 基本安装(推荐)

对于大多数网页爬取和数据抓取任务,可以直接使用pip进行安装:

pip install crawl4ai

这样,默认安装的是Crawl4AI的异步版本,使用Playwright进行网页爬取。

如果安装时遇到Playwright相关错误,可以通过以下命令手动安装Playwright:

playwright install

或者,安装特定版本的Chromium:

python -m playwright install chromium

3.2 同步版本安装

如果需要使用Selenium的同步版本,可以使用以下命令:

pip install crawl4ai[sync]

3.3 开发者安装

对于想要参与项目开发,修改源代码的贡献者,可以通过以下步骤进行安装:

git clone https://github.com/unclecode/crawl4ai.git
cd crawl4ai
pip install -e .

4.高级应用

要想充分发挥Crawl4AI的能力,可以看看这些高级功能和应用案例:

4.1 执行JavaScript和使用CSS选择器

可以利用Crawl4AI执行自定义JavaScript代码,以及通过CSS选择器精准定位页面元素,从而提升爬取任务的效率和精确度。这让你能够更灵活地处理复杂的网页数据抓取需求。

import asyncio
from crawl4ai import AsyncWebCrawler

asyncdef main():
    asyncwith AsyncWebCrawler(verbose=True) as crawler:
        js_code = [
            "const loadMoreButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.includes('Load More')); loadMoreButton && loadMoreButton.click();"
        ]
        result = await crawler.arun(
            url="https://www.nbcnews.com/business",
            js_code=js_code,
            css_selector="article.tease-card",
            bypass_cache=True
        )
        print(result.extracted_content)

if __name__ == "__main__":
    asyncio.run(main())

4.2 使用代理

通过将爬取任务路由到代理,增强隐私和访问权限。

import asyncio
from crawl4ai import AsyncWebCrawler

async def main():
    async with AsyncWebCrawler(verbose=True, proxy="http://127.0.0.1:7890") as crawler:
        result = await crawler.arun(
            url="https://www.nbcnews.com/business",
            bypass_cache=True
        )
        print(result.markdown)

if __name__ == "__main__":
    asyncio.run(main())

4.3 不使用 LLM 提取结构化数据

使用JsonCssExtractionStrategy精确提取使用 CSS 选择器的结构化数据。

import asyncio
import json
from crawl4ai import AsyncWebCrawler
from crawl4ai.extraction_strategy import JsonCssExtractionStrategy

asyncdef extract_news_teasers():
    schema = {
        "name": "News Teaser Extractor",
        "baseSelector": ".wide-tease-item__wrapper",
        "fields": [
            {"name": "category", "selector": ".unibrow span[data-testid='unibrow-text']", "type": "text"},
            {"name": "headline", "selector": ".wide-tease-item__headline", "type": "text"},
            {"name": "summary", "selector": ".wide-tease-item__description", "type": "text"},
            {"name": "time", "selector": "[data-testid='wide-tease-date']", "type": "text"},
            {
                "name": "image",
                "type": "nested",
                "selector": "picture.teasePicture img",
                "fields": [
                    {"name": "src", "type": "attribute", "attribute": "src"},
                    {"name": "alt", "type": "attribute", "attribute": "alt"},
                ],
            },
            {"name": "link", "selector": "a[href]", "type": "attribute", "attribute": "href"},
        ],
    }
    extraction_strategy = JsonCssExtractionStrategy(schema, verbose=True)
    asyncwith AsyncWebCrawler(verbose=True) as crawler:
        result = await crawler.arun(
            url="https://www.nbcnews.com/business",
            extraction_strategy=extraction_strategy,
            bypass_cache=True,
        )
        assert result.success, "Failed to crawl the page"
        news_teasers = json.loads(result.extracted_content)
        print(f"Successfully extracted {len(news_teasers)} news teasers")
        print(json.dumps(news_teasers[0], indent=2))

if __name__ == "__main__":
    asyncio.run(extract_news_teasers())

4.4 使用 OpenAI 提取结构化数据

利用 OpenAI 的能力动态提取和结构化数据:

import os
import asyncio
from crawl4ai import AsyncWebCrawler
from crawl4ai.extraction_strategy import LLMExtractionStrategy
from pydantic import BaseModel, Field

class OpenAIModelFee(BaseModel):
    model_name: str = Field(..., description="Name of the OpenAI model.")
    input_fee: str = Field(..., description="Fee for input token for the OpenAI model.")
    output_fee: str = Field(..., description="Fee for output token for the OpenAI model.")

asyncdef main():
    asyncwith AsyncWebCrawler(verbose=True) as crawler:
        result = await crawler.arun(
            url='https://openai.com/api/pricing/',
            word_count_threshold=1,
            extraction_strategy=LLMExtractionStrategy(
                provider="openai/gpt-4
o",
                api_token=os.getenv('OPENAI_API_KEY'), 
                schema=OpenAIModelFee.schema(),
                extraction_type="schema",
                instruction="""From the crawled content, extract all mentioned model names along with their fees for input and output tokens. 

Do not miss any models in the entire content. One extracted model JSON format should look like this: 

{"model_name": "GPT-4", "input_fee": "US$10.00 / 1M tokens", "output_fee": "US$30.00 / 1M tokens"}."""
            ),            
            bypass_cache=True,
        )
        print(result.extracted_content)

if __name__ == "__main__":
    asyncio.run(main())

4.5 会话管理 & 动态内容爬取

处理复杂的场景,如爬取通过 JavaScript 加载动态内容的多个页面:

import asyncio
import re
from bs4 import BeautifulSoup
from crawl4ai import AsyncWebCrawler

asyncdef crawl_typescript_commits():
    first_commit = ""
    asyncdef on_execution_started(page):
        nonlocal first_commit 
        try:
            whileTrue:
                await page.wait_for_selector('li.Box-sc-g0xbh4-0 h4')
                commit = await page.query_selector('li.Box-sc-g0xbh4-0 h4')
                commit = await commit.evaluate('(element) => element.textContent')
                commit = re.sub(r'\s+', '', commit)
                if commit and commit != first_commit:
                    first_commit = commit
                    break
                await asyncio.sleep(0.5)
        except Exception as e:
            print(f"Warning: New content didn't appear after JavaScript execution: {e}")
    asyncwith AsyncWebCrawler(verbose=True) as crawler:
        crawler.crawler_strategy.set_hook('on_execution_started', on_execution_started)
        url = "https://github.com/microsoft/TypeScript/commits/main"
        session_id = "typescript_commits_session"
        all_commits = []
        js_next_page = """
        const button = document.querySelector('a[data-testid="pagination-next-button"]');
        if (button) button.click();
        """
        for page in range(3):  # Crawl 3 pages
            result = await crawler.arun(
                url=url,
                session_id=session_id,
                css_selector="li.Box-sc-g0xbh4-0",
                js=js_next_page if page > 0elseNone,
                bypass_cache=True,
                js_only=page > 0
            )
            assert result.success, f"Failed to crawl page {page + 1}"
            soup = BeautifulSoup(result.cleaned_html, 'html.parser')
            commits = soup.select("li.Box-sc-g0xbh4-0")
            all_commits.extend(commits)
            print(f"Page {page + 1}: Found {len(commits)} commits")
        await crawler.crawler_strategy.kill_session(session_id)
        print(f"Successfully crawled {len(all_commits)} commits across 3 pages")

if __name__ == "__main__":
    asyncio.run(crawl_typescript_commits())

5.性能对比

Crawl4AI在设计上注重速度和效率,性能持续超越许多付费服务。以下是性能测试结果:

要点总结:

  • 简单爬取:Crawl4AI的速度是Firecrawl的4倍以上。

  • 带JavaScript执行:即使在执行JavaScript加载更多内容的情况下(图片数量增加一倍),Crawl4AI的速度依然远超Firecrawl的简单爬取。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2328015.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【爬虫开发】爬虫开发从0到1全知识教程第14篇:scrapy爬虫框架,介绍【附代码文档】

本教程的知识点为:爬虫概要 爬虫基础 爬虫概述 知识点: 1. 爬虫的概念 requests模块 requests模块 知识点: 1. requests模块介绍 1.1 requests模块的作用: 数据提取概要 数据提取概述 知识点 1. 响应内容的分类 知识点&#xff1a…

SQLark:一款国产免费数据库开发和管理工具

SQLark(百灵连接)是一款面向信创应用开发者的数据库开发和管理工具,用于快速查询、创建和管理不同类型的数据库系统,目前可以支持达梦数据库、Oracle 以及 MySQL。 对象管理 SQLark 支持丰富的数据库对象管理功能,包括…

防爆对讲机VS非防爆对讲机,如何选择?

在通信设备的广阔市场中,对讲机以其高效、便捷的特点,成为众多行业不可或缺的沟通工具。而面对防爆对讲机与非防爆对讲机,许多用户常常陷入选择困境。究竟该如何抉择,且听我为您细细道来。 防爆对讲机,专为危险作业场…

微信小程序开发:开发实践

微信小程序开发实践研究 摘要 随着移动互联网的迅猛发展,微信小程序作为一种轻量化、无需安装的应用形式,逐渐成为开发者和用户的首选。本文以“个人名片”小程序为例,详细阐述了微信小程序的开发流程,包括需求分析、项目规划、…

操作 Office Excel 文档类库Excelize

Excelize 是 Go 语言编写的一个用来操作 Office Excel 文档类库,基于 ECMA-376 OOXML 技术标准。可以使用它来读取、写入 XLSX 文件,相比较其他的开源类库,Excelize 支持操作带有数据透视表、切片器、图表与图片的 Excel 并支持向 Excel 中插…

青铜与信隼的史诗——TCP与UDP的千年博弈

点击下面图片带您领略全新的嵌入式学习路线 🔥爆款热榜 88万阅读 1.6万收藏 第一章 契约之匣与自由之羽 熔岩尚未冷却的铸造台上,初代信使长欧诺弥亚将液态秘银倒入双生模具。左侧模具刻着交握的青铜手掌,右侧则是展开的隼翼纹章。当星辰…

「青牛科技」GC5849 12V三相无感正弦波电机驱动芯片

芯片描述: • 4 ~ 20V 工作电压, 30V 最大耐压 • 驱动峰值电流 2.0A ,连续电流 800mA 以内 • 芯片内阻: 900mΩ (上桥 下桥) • eSOP-8 封装,底部 ePAD 散热,引…

Java基础之反射的基本使用

简介 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意属性和方法;这种动态获取信息以及动态调用对象方法的功能称为Java语言的反射机制。反射让Java成为了一门动…

大语言模型中的嵌入模型

本教程将拆解什么是嵌入模型、为什么它们在NLP中如此重要,并提供一个简单的Python实战示例。 分词器将原始文本转换为token和ID,而嵌入模型则将这些ID映射为密集向量表示。二者合力为LLMs的语义理解提供动力。图片来源:[https://tzamtzis.gr/2024/coding/tokenization-by-an…

【从零实现Json-Rpc框架】- 项目实现 - 服务端主题实现及整体封装

📢博客主页:https://blog.csdn.net/2301_779549673 📢博客仓库:https://gitee.com/JohnKingW/linux_test/tree/master/lesson 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! &…

开源的 LLM 应用开发平台Dify的安装和使用

文章目录 前提环境应用安装deocker desktop镜像源配置Dify简介Dify本地docker安装Dify安装ollama插件Dify安装硅基流动插件简单应用练习进阶应用练习数据库图像检索与展示助手echart助手可视化 前提环境 Windows环境 docker desktop魔法环境:访问Dify项目ollama电脑…

从零构建大语言模型全栈开发指南:第五部分:行业应用与前沿探索-5.1.2行业落地挑战:算力成本与数据隐私解决方案

👉 点击关注不迷路 👉 点击关注不迷路 👉 点击关注不迷路 文章大纲 从零构建大语言模型全栈开发指南-第五部分:行业应用与前沿探索5.1.2 行业落地挑战:算力成本与数据隐私解决方案1. 算力成本挑战与优化策略1.1 算力成本的核心问题1.2 算力优化技术方案2. 数据隐私挑战…

NodeJS--NPM介绍使用

1、使用npm install命令安装模块 1.1、本地安装 npm install express 1.2、全局安装 npm install express -g 1.3、本地安装和全局安装的区别

DeepSeek与ChatGPT的优势对比:选择合适的工具来提升工作效率

选DeepSeek还是ChatGPT?这就像问火锅和披萨哪个香! "到底该用DeepSeek还是ChatGPT?” 这个问题最近在互联网圈吵翻天!其实这就跟选手机系统-样,安卓党iOS党都能说出一万条理由,但真正重要的是你拿它来干啥!&am…

25大唐杯赛道一本科B组知识点大纲(下)

5G/6G网络技术知识点(10%) 工程概论及通信工程项目实践(20%) 5G垂直行业应用知识点(20%) ⭐⭐⭐为重点知识,尽量要过一遍哦 大唐杯赛道一国一备赛思路 大唐杯国一省赛回忆录--有付出就会有收…

Python+Playwright自动化测试-1-环境准备与搭建

1、Playwright 是什么? 微软在 2020 年初开源的新一代自动化测试工具,它的功能类似于 Selenium、Pyppeteer 等,都可以驱动浏览器进行各种自动化操作。它的功能也非常强大,对市面上的主流浏览器都提供了支持,API 功能简…

生产管理系统如何破解汽车零部件行业追溯难痛点

在汽车零部件制造行业中,生产追溯一直是企业面临的核心挑战之一。随着市场竞争的加剧和客户需求的日益复杂,如何确保产品质量、快速定位问题源头、减少批次性返工,成为了每个企业亟待解决的问题。而生产管理系统,作为智能制造的重…

【XTerminal】【树莓派】Linux系统下的函数调用编程

目录 一、XTerminal下的Linux系统调用编程 1.1理解进程和线程的概念并在Linux系统下完成相应操作 (1) 进程 (2)线程 (3) 进程 vs 线程 (4)Linux 下的实践操作 1.2Linux的“虚拟内存管理”和stm32正式物理内存(内存映射)的区别 (1)Linux虚拟内存管…

umi框架开发移动端h5

1、官网:https://umijs.org/ 2、创建出来的项目 yarn create umi yarn start3、推荐目录结构 . ├── config │ └── config.ts ├── public//静态资源 ├── dist ├── mock │ └── app.ts|tsx ├── src │ ├── .umi │ ├── .um…

3.9/Q2,Charls最新文章解读

文章题目:Association between remnant cholesterol and depression in middle-aged and older Chinese adults: a population-based cohort study DOI:10.3389/fendo.2025.1456370 中文标题:中国中老年人残留胆固醇与抑郁症的关系&#xff1…