微信扫码
添加专属顾问
 
                        我要投稿
今天我要跟大家分享一个超级棒的开源工具——Crawl4AI。这个工具简直是构建AI Agent的福音,它自动化了网页爬取和数据提取的过程,让开发者们能更高效地构建智能Agent来收集和分析信息。
首先,Crawl4AI是完全开源且免费的,这意味着开发者们可以无门槛地使用它。它的核心亮点是AI驱动,能够自动识别和解析网页元素,大大节省了我们的时间和精力。而且,Crawl4AI还能将提取的数据转换成结构化的格式,比如JSON或markdown,让数据分析变得简单多了。
接下来,我给大家简单介绍一下如何使用Crawl4AI。首先,你需要安装它,命令很简单,一行代码就搞定。然后,创建一个Python脚本,初始化网络爬虫,从URL提取数据。Crawl4AI还支持滚动浏览、多个URL爬取、媒体标签提取、元数据提取,甚至是截图功能,功能非常全面。
from crawl4ai import WebCrawler
crawler = WebCrawler()
crawler.warmup()
result = crawler.run(url="https://openai.com/api/pricing/")
print(result.markdown)
重点来了,Crawl4AI还能用大型语言模型(LLM)来定义提取策略,把提取的数据转换成结构化格式。这意味着,你可以根据需要定制数据提取的规则,让Crawl4AI按照你的指示去抓取网页上的信息。
mport os
from crawl4ai import WebCrawler
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.")
url = 'https://openai.com/api/pricing/'
crawler = WebCrawler()
crawler.warmup()
result = crawler.run(
        url=url,
        word_count_threshold=1,
        extraction_strategy= LLMExtractionStrategy(
            provider= "openai/gpt-4o", api_token = os.getenv('OPENAI_API_KEY'), 
            schema=OpenAIModelFee.schema(),
            extraction_type="schema",
            instruction="""从爬取的内容中,提取所有提到的模型名称以及它们的输入和输出token费用。  不要遗漏整个内容中的任何模型。一个提取的模型JSON格式应如下所示:  
{"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)
更厉害的是,Crawl4AI可以和Praison CrewAI集成,让数据的处理更加高效。你可以创建一个工具文件,包装Crawl工具,然后配置AI Agent使用Crawl进行网页抓取和数据提取。
举个例子,你可以设置一个AI Agent,它的角色是网页抓取专家,专门负责从网上抓取模型定价信息。另一个Agent可能是数据清洗专家,确保收集的数据准确无误,格式规范。还有一个Agent是数据分析专家,专注于从数据中提取有价值的洞察。
import os
from crawl4ai import WebCrawler
from crawl4ai.extraction_strategy import LLMExtractionStrategy
from pydantic import BaseModel, Field
from praisonai_tools import BaseTool
class ModelFee(BaseModel):
    llm_model_name: str = Field(..., description="Name of the model.")
    input_fee: str = Field(..., description="Fee for input token for the model.")
    output_fee: str = Field(..., description="Fee for output token for the model.")
class ModelFeeTool(BaseTool):
    name: str = "ModelFeeTool"
    description: str = "从给定的定价页面中提取模型的输入和输出token费用。 "
    def _run(self, url: str):
        crawler = WebCrawler()
        crawler.warmup()
        result = crawler.run(
            url=url,
            word_count_threshold=1,
            extraction_strategy= LLMExtractionStrategy(
                provider="openai/gpt-4o",
                api_token=os.getenv('OPENAI_API_KEY'), 
                schema=ModelFee.schema(),
                extraction_type="schema",
                instruction="""从爬取的内容中,提取所有提到的模型名称以及它们的输入和输出token费用。  不要遗漏整个内容中的任何模型。一个提取的模型JSON格式应如下所示:  
{"model_name": "GPT-4", "input_fee": "US$10.00 / 1M tokens", "output_fee": "US$30.00 / 1M tokens"}。"""
            ),            
            bypass_cache=True,
        )
        return result.extracted_content
if __name__ == "__main__":
    # Test the ModelFeeTool
    tool = ModelFeeTool()
    url = "https://www.openai.com/pricing"
    result = tool.run(url)
    print(result)
配置yaml
framework: crewai
topic: extract model pricing from websites
roles:
  web_scraper:
    backstory: 一个网络爬虫专家,对从在线资源中提取结构化数据有深刻的理解。https://openai.com/api/pricing/ https://www.anthropic.com/pricing https://cohere.com/pricing
    goal: 从各种网站收集模型定价数据
    role: Web Scraper
    tasks:
      scrape_model_pricing:
        description: 从提供的网站列表中抓取模型定价信息。
        expected_output: 包含模型定价数据的原始HTML或JSON。
    tools:
    - 'ModelFeeTool'
  data_cleaner:
    backstory: 数据清洗专家,确保所有收集的数据准确无误且格式正确。
    goal: 清洗并整理抓取到的定价数据
    role: Data Cleaner
    tasks:
      clean_pricing_data:
        description: 处理原始抓取数据,删除任何重复项和不一致项,并将其转换为结构化格式。
        expected_output: 包含模型定价的已清洗且已整理的JSON或CSV文件
          data.
    tools:
    - ''
  data_analyzer:
    backstory: 数据分析专家,专注于从结构化数据中获取可操作的见解。
    goal: 分析已清洗的定价数据以提取见解
    role: Data Analyzer
    tasks:
      analyze_pricing_data:
        description: 分析已清洗的数据,提取模型定价的趋势、模式和见解。
        expected_output: 总结模型定价趋势和见解的详细报告。
    tools:
    - ''
dependencies: []
总之,Crawl4AI是一个强大的工具,它让AI Agent能够以更高的效率和准确性执行网页爬取和数据提取任务。它的开源特性、AI驱动的能力以及多功能性,对于想要构建智能且数据驱动的Agent的开发者来说,绝对是一个宝贵的资源。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-31
Google DeepMind揭秘:开源AI模型如何泄露训练秘方
2025-10-31
有人问我会不会用 AI,我直接拿出这个 Ollama + FastGPT 项目给他看
2025-10-30
开源可信MCP,AICC机密计算新升级!
2025-10-30
OpenAI 开源了推理安全模型-gpt-oss-safeguard-120b 和 gpt-oss-safeguard-20b
2025-10-29
刚刚,OpenAI 再次开源!安全分类模型 gpt-oss-safeguard 准确率超越 GPT-5
2025-10-29
AI本地知识库+智能体系列:手把手教你本地部署 n8n,一键实现自动采集+智能处理!
2025-10-29
n8n如何调用最近爆火的deepseek OCR?
2025-10-29
OpenAI终于快要上市了,也直面了这23个灵魂拷问。
 
            2025-08-20
2025-09-07
2025-08-05
2025-08-20
2025-08-26
2025-08-22
2025-09-06
2025-08-06
2025-10-20
2025-08-22
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17
2025-09-09
2025-09-08
2025-09-07