Crawl4AI 简化了网络爬虫和数据提取,使其适用于大型语言模型(LLMs)和人工智能应用。🆓🌐
功能、性能和跨平台兼容性方面的重大改进!🚀
✨ 访问我们的文档网站
✨ 查看演示
非常感谢这些杰出的贡献者,使这个版本成为可能:
你们的贡献正在推动 Crawl4AI 向前发展!🚀
from crawl4ai import WebCrawler # 创建 WebCrawler 实例 crawler = WebCrawler() # 预热爬虫(加载必要的模型) crawler.warmup() # 在 URL 上运行爬虫 result = crawler.run(url="https://www.nbcnews.com/business") # 打印提取的内容 print(result.markdown)
virtualenv venv source venv/bin/activate pip install "crawl4ai @ git+https://github.com/unclecode/crawl4ai.git"
# 对于 Mac 用户(M1/M2) # docker build --platform linux/amd64 -t crawl4ai . docker build -t crawl4ai . docker run -d -p 8000:80 crawl4ai
docker pull unclecode/crawl4ai:latest docker run -d -p 8000:80 unclecode/crawl4ai:latest
这个库最重要的设计原则也许就是速度。我们需要确保它能够尽可能快速地并行处理多个链接和资源。将这种速度与 Groq 等快速 LLM 结合起来,结果将会令人惊叹。
import time from crawl4ai.web_crawler import WebCrawler crawler = WebCrawler() crawler.warmup() start = time.time() url = r"https://www.nbcnews.com/business" result = crawler.run( url, word_count_threshold=10, bypass_cache=True) end = time.time() print(f"耗时:{end - start}")
让我们看看上面代码片段的计算时间:
[日志] 🚀 爬取完成,成功:True,耗时:1.3623387813568115 秒 [日志] 🚀 内容提取完成,成功:True,耗时:0.05715131759643555 秒 [日志] 🚀 提取过程,耗时:0.05750393867492676 秒。 总耗时:1.439958095550537
从页面获取内容花费了 1.3623 秒,提取内容花费了 0.0575 秒。🚀
从官方页面爬取所有 OpenAI 模型及其费用。
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 模型的输入 token 费用。") output_fee: str = Field(..., description="OpenAI 模型的输出 token 费用。") 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)