Crawl4AI,智能體網(wǎng)絡(luò)自動采集利器
Crawl是一款免費的開源工具,利用AI技術(shù)簡化網(wǎng)絡(luò)爬取和數(shù)據(jù)提取,提高信息收集與分析的效率。它智能識別網(wǎng)頁內(nèi)容,并將數(shù)據(jù)轉(zhuǎn)換為易于處理的格式,功能全面且操作簡便。
1 使用 Crawl 的步驟
步驟 1:安裝與設(shè)置
pip install “crawl4ai @ git+https://github.com/unclecode/crawl4ai.git" transformers torch nltk
步驟 2:數(shù)據(jù)提取
創(chuàng)建Python腳本,啟動網(wǎng)絡(luò)爬蟲并從URL提取數(shù)據(jù):
from crawl4ai import WebCrawler
# 創(chuàng)建 WebCrawler 的實例
crawler = WebCrawler()
# 預熱爬蟲(加載必要的模型)
crawler.warmup()
# 在 URL 上運行爬蟲
result = crawler.run(url="https://openai.com/api/pricing/")
# 打印提取的內(nèi)容
print(result.markdown)
步驟 3:數(shù)據(jù)結(jié)構(gòu)化
使用LLM(大型語言模型)定義提取策略,將數(shù)據(jù)轉(zhuǎn)換為結(jié)構(gòu)化格式:
import 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="""從爬取的內(nèi)容中提取所有提到的模型名稱以及它們的輸入和輸出令牌費用。不要遺漏整個內(nèi)容中的任何模型。提取的模型 JSON 格式應(yīng)該像這樣:
{"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 智能體集成,實現(xiàn)高效的數(shù)據(jù)處理:
pip install praisonai
創(chuàng)建工具文件(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="""從爬取的內(nèi)容中提取所有提到的模型名稱以及它們的輸入和輸出令牌費用。不要遺漏整個內(nèi)容中的任何模型。提取的模型 JSON 格式應(yīng)該像這樣:
{"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工具進行網(wǎng)絡(luò)抓取和數(shù)據(jù)提取。在crewai框架下,我們設(shè)定了三個核心角色,共同完成網(wǎng)站模型定價信息的提取任務(wù):
- 網(wǎng)絡(luò)爬蟲:負責從OpenAI、Anthropic和Cohere等網(wǎng)站抓取定價信息,輸出原始HTML或JSON數(shù)據(jù)。
- 數(shù)據(jù)清理員:確保收集的數(shù)據(jù)準確無誤,并整理成結(jié)構(gòu)化的JSON或CSV文件。
- 數(shù)據(jù)分析員:分析清理后的數(shù)據(jù),提煉出定價趨勢和模式,并編制詳細報告。
整個流程無需額外依賴,各角色獨立完成各自任務(wù)。
2 AI 智能體應(yīng)用實例
以Crawl為基礎(chǔ),Praison-AI智能體能夠執(zhí)行網(wǎng)絡(luò)抓取、數(shù)據(jù)清洗和分析工作。它們相互協(xié)作,從多個網(wǎng)站抓取定價數(shù)據(jù),并匯總成詳盡的報告,以展示分析結(jié)果。
3 結(jié)語
Crawl是一個強大的工具,它賦予AI智能體更高的效率和準確性執(zhí)行網(wǎng)絡(luò)爬取和數(shù)據(jù)提取任務(wù)。其開源特性、AI驅(qū)動的能力和多功能性,使其成為構(gòu)建智能且數(shù)據(jù)驅(qū)動智能體的寶貴資產(chǎn)。
