搞定网页爬取和数据提取?Crawl4AI带你体验高效AI Agent工作流程
先说一个核心结论:对于正在构建AI Agent的开发者来说,Crawl4AI这个开源工具的出现,意味着网页爬取和数据提取这件事终于变得不那么头疼了。它本质上是在帮你自动化地完成“看懂网页、提取信息”这个流程——而且完全不用花钱。

既然说开源免费,那门槛基本为零。Crawl4AI最核心的优势在于它用AI驱动的方式来自动识别页面元素,而不是像传统爬虫那样靠繁琐的规则去匹配。这一步节省的不仅是时间,更多是精力——尤其是当你面对各种风格迥异的网页时。而且,它能把最终提取出来的数据直接转成结构化的格式,比如JSON或Markdown,这意味着从数据到分析,中间少了一道很麻烦的转换环节。
那么,具体怎么用?其实步骤非常精简:
安装只一行命令,然后创建一个Python脚本,初始化爬虫,从URL开始干活。值得注意的是,Crawl4AI还支持滚动浏览、多URL并行爬取、媒体标签和元数据提取,甚至内置了截图功能——这些功能单独拎出来任何一个都够传统的爬虫折腾半天。
from crawl4ai import WebCrawler
crawler = WebCrawler()
crawler.warmup()
result = crawler.run(url="https://openai.com/api/pricing/")
print(result.markdown)
真正值得关注的亮点,是它能配合大型语言模型(LLM)来定义提取策略。就是说,你可以告诉Crawl4AI:“从这个页面里,按我给的模板提取特定信息。”这种智能化程度,已经远远超出了“爬数据”本身——它更像是给爬虫装了一个能看懂内容的“脑子”。
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="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进行深度集成。这个场景很有意思:假设你设置一个AI Agent群,其中一位是“网页抓取专家”,专门负责从定价页面捞数据;另一位是“数据清洗专家”,确保捞回来的东西格式整齐、没有重复;再来一位“数据分析专家”,负责从清洗后的数据里提炼出有价值的结论。三者在分工协作中,整个从采集到洞察的链路就被打通了。
下面是一段集成了工具类的示例代码,展示了如何把Crawl4AI封装成一个可供多个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__":
tool = ModelFeeTool()
url = "https://www.openai.com/pricing"
result = tool.run(url)
print(result)
对应的Agent团队配置如下(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提供了一种高效且精准的“数据获取骨架”。开源、智能、可深度定制——对于那些希望让Agent真正理解网页内容而非只是抓取页面的开发者来说,这无疑是一个相当值得投入时间的资源。
-
- 关于宇宙的好的网名有哪些
- 角色扮演 | 1
- 网名