AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


搞定网页爬取和数据提取?Crawl4AI带你体验高效AI Agent工作流程
发布日期:2024-07-27 08:33:26 浏览次数: 1796


今天我要跟大家分享一个超级棒的开源工具——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+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询