Crawl是一个强大的工具,它赋予AI智能体更高的效率和准确性执行网络爬取和数据提取任务。其开源特性、AI驱动的能力和多功能性,使其成为构建智能且数据驱动智能体的宝贵资产,告别繁琐: 爬虫新宠 crawl4ai,数行代码搞定数据采集,AI 爬虫核武器!Crawl4AI 横空出世,数据采集只需一行代码,Crawl4AI 是一款专为大语言模型(LLM)和 AI 应用设计的开源网页爬虫与数据抓取工具。它不仅能高效采集网页数据,还能直接输出结构化、干净的 Markdown 内容。
1 使用 Crawl 的步骤
步骤 1:安装与设置
pip install "crawl4ai @ git+https://github.com/unclecode/crawl4ai.git" transformers torch nltk
步骤 2:数据提取
创建Python脚本,启动网络爬虫并从URL提取数据:
from crawl4ai import WebCrawler
# 创建 WebCrawler 的实例 crawler = WebCrawler()
# 预热爬虫(加载必要的模型) crawler.warmup()
# 在 URL 上运行爬虫 result = crawler.run(url="https://openai.com/api/pricing/")
# 打印提取的内容 print(result.markdown)
步骤 3:数据结构化
使用LLM(大型语言模型)定义提取策略,将数据转换为结构化格式:
iimport os
from crawl4ai import WebCrawler
from crawl4ai.extraction_strategy import LLMExtractionStrategy
from pydantic import BaseModel, Field
class OpenAIModelFee(BaseModel):
model_name: str = Field(..., description="OpenAI 模型的名称。")
input_fee: str = Field(..., description="OpenAI 模型的输入令牌费用。")
output_fee: str = Field(..., description="OpenAI 模型的输出令牌费用。")
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="""从爬取的内容中提取所有提到的模型名称以及它们的输入和输出令牌费用。不要遗漏整个内容中的任何模型。提取的模型 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)
步骤 4:集成AI智能体
将 Crawl 与 Praison CrewAI 智能体集成,实现高效的数据处理:
pip install praisonai
创建工具文件(tools.py)来包装 Crawl 工具:
# tools.py
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="模型的名称。")
input_fee: str = Field(..., description="模型的输入令牌费用。")
output_fee: str = Field(..., description="模型的输出令牌费用。")
class ModelFeeTool(BaseTool):
name: str = "ModelFeeTool"
description: str = "从给定的定价页面提取模型的费用信息。"
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="""从爬取的内容中提取所有提到的模型名称以及它们的输入和输出令牌费用。不要遗漏整个内容中的任何模型。提取的模型 JSON 格式应该像这样:
{"llm_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__":
# 测试 ModelFeeTool
tool = ModelFeeTool()
url = "https://www.openai.com/pricing"
result = tool.run(url)
print(result)
AI智能体配置
配置AI智能体使用Crawl工具进行网络抓取和数据提取。在crewai框架下,我们设定了三个核心角色,共同完成网站模型定价信息的提取任务:
网络爬虫:负责从OpenAI、Anthropic和Cohere等网站抓取定价信息,输出原始HTML或JSON数据。
数据清理员:确保收集的数据准确无误,并整理成结构化的JSON或CSV文件。
数据分析员:分析清理后的数据,提炼出定价趋势和模式,并编制详细报告。
整个流程无需额外依赖,各角色独立完成各自任务。
2 AI 智能体应用实例
以Crawl为基础,Praison-AI智能体能够执行网络抓取、数据清洗和分析工作。它们相互协作,从多个网站抓取定价数据,并汇总成详尽的报告,以展示分析结果。
github地址:https://github.com/unclecode/crawl4ai
文档地址:https://docs.crawl4ai.com