官方链接:
Github :https://github.com/unclecode/crawl4ai
文档主页:https://docs.crawl4ai.com/
当前版本:Crawl4AI v0.5.0
主要新功能:
- 可配置策略(广度优先、深度优先、最佳优先)探索整个网站。
- 根据可用内存动态调解并发性。
- 可 Docker 部署
- 有命令行界面 (CLI)
- LLM配置 (LLMConfig):方便 LLM模型用于提取、过滤和模式生成。
次要更新和改进:
- LXML爬取模式:使用LXMLWebScrapingStrategy举行更快的HTML解析。
- 署理轮换:添加了ProxyRotationStrategy,并实现了RoundRobinProxyStrategy。
- PDF处理:从PDF文件中提取文本、图像和元数据。
- URL重定向跟踪:自动跟踪并记录重定向。
- 遵守Robots.txt:可选择性地遵守网站爬取规则。
- LLM驱动的模式生成:使用LLM自动创建提取模式。
- LLMContentFilter:使用LLM生成高质量、重点突出的markdown。
- 改进的错误处理和稳固性:浩繁错误修复和性能加强。
- 加强文档:更新指南和示例。
安装 Crawl4AI
体系环境:
- Windows 11 Professional 与 Ubuntu24
- Python 3.12
安装过程:
1. 底子安装
在 Windows 11 CMD
在 Ubuntu24 中运行:
- python3 -m venv -venv
- source venv/bin/activate
- pip install crawl4ai
复制代码 安装焦点库与根本依靠内容,不包罗任何高级功能。
装了一堆:
2. 自动设置
自动完成浏览器安装、体系兼容性检查和环境验证:
Ubuntu 24:
Windows 11:
3. 校验安装
- import asyncio
- from crawl4ai import AsyncWebCrawler, BrowserConfig, CrawlerRunConfig
- async def main():
- async with AsyncWebCrawler() as crawler:
- result = await crawler.arun(
- url="https://www.google.com",
- )
- print(result.markdown[:300]) # Show the first 300 characters of extracted text
- if __name__ == "__main__":
- asyncio.run(main())
复制代码 正常是:
4. 安装诊断命令
使用 Crawl4AI
根本的 CLI 方式:
1. 指定格式输出,参数:-o 或 --output [all|json|markdown|md|markdown-fit|md-fit]
- crwl https://docs.crawl4ai.com/ -o markdown
复制代码
2. 不使用已经存在的缓存,从服务器获取最新内容,参数: -b , --bypass-cache
- crwl https://docs.crawl4ai.com/ --bypass-cache
复制代码- crwl https://docs.crawl4ai.com/ -b
复制代码
3. 带有详细的运行信息和日志, 参数: -v, --verbose
详细信息:
- 爬取的每个步骤
- 请求和响应的详情
- 遇到的问题或警告
- 处理过程中的状态变化
- 性能相干的信息(如加载时间)
对于调试问题或相识步伐的工作方式特别有效。如网站爬取失败或效果不符合预期,verbose 模式可以帮助你确定问题出在那里。
- crwl https://docs.crawl4ai.com/ -v
复制代码- crwl https://docs.crawl4ai.com/ --verbose
复制代码
4. 指定提取策略的文件 参数: -e, --extraction-config [PATH]
- crwl https://docs.crawl4ai.com/ -e [策略文件路径]
复制代码- crwl https://docs.crawl4ai.com/ --extraction-config [策略文件路径]
复制代码 自定义爬虫如何从网页中提取内容的详细规则和策略,可以准确控制数据提取,使用YAML或JSON格式结构。
1) 提取策略配置文件的主要功能
- 指定使用哪种范例的选择器(CSS、XPath、正则表达式等)来定位页面元素
- 定义如那边理和转换提取的原始内容
- 删除多余空格、HTML标签
- 将价格文本转为数字
- 内容分割和合并
- 支持嵌套数据结构的提取
- 先定位父元素,再从父元素中提取子元素
- 处理列表、表格等复杂结构
- 指定条件,决定是否提取内容
- 定义在多页面间导航以提取内容
2) 例:从Amazon产物页面提取DEWALT电动工具的各种信息
主要提取内容
- 根本信息:标题、品牌、型号、ASIN
- 价格信息:当前价格、原价、优惠状态
- 产物图片:主图及全部备选图片
- 技术规格:电压、扭矩、重量、尺寸等
- 产物特点:全部列出的产物特性
- 评论数据:平均评分和评论数目
- 可用性:库存状态和配送选项
- 分类信息:主类别和面包屑导航中的子类别
- 相干产物:经常一起购买的商品
文件名:C:\temp\dewalt-extraction-strategy.yaml
运行:
- crwl https://a.co/d/17HZeGj -e dewalt-extraction-strategy.yaml -s dewalt-schema.json
复制代码 由于:
大概是太频繁,或我的 VPN 有在黑名单,没抓到数据。
5. 指定一个JSON模式(JSON schema)文件的路径 参数:-s, --schema [PATH]
- crwl https://docs.crawl4ai.com/ -s [JSON schema 文件的路径]
复制代码- crwl https://docs.crawl4ai.com/ --schema [JSON schema 文件的路径]
复制代码 可以用 JSON schema (该模式) 文件来定义:从网站提取内容时的结构化的数据格式,如:
- 从网页中提取的数据的结构、属性和数据范例。它作为一个"模板",告诉爬虫工具应该提取哪些信息以及如何组织这些信息。
- 当爬虫处理网页时,它会根据提供的JSON模式来识别和提取符合该模式定义的数据元素,然后将它们组织成符合模式结构的输出。
例:从 Amazon.com 抓取 电动工具
从 Amazon.com 抓取 电动工具:DEWALT ATOMIC 20V MAX* 3/8 in. Cordless Impact Wrench with Hog Ring Anvil (Tool Only) (DCF923B)
链接(商品): https://a.co/d/cfTKG4j
内容包罗:
- 根本产物信息:标题、品牌、型号和ASIN号
- 价格信息:当前价格、原价和扣头
- 产物图片:全部产物图片的URL数组
- 技术规格:电压、扭矩、速率、重量、尺寸等
- 产物特点:产物特性和上风的列表
- 分类信息:主类别和子类别
- 评论数据:平均评分、评论数目和评分分布
- 可用性信息:库存状态和配送选项
- 保修信息:产物保修详情
- 相干产物:兼容配件和经常一起购买的产物
文件名:C:\temp\dewalt-schema.json
- {
- "$schema": "http://json-schema.org/draft-07/schema#",
- "title": "Amazon Power Tool Product Schema",
- "description": "Schema for extracting DEWALT impact wrench product details from Amazon",
- "type": "object",
- "properties": {
- "productBasicInfo": {
- "type": "object",
- "properties": {
- "title": {
- "type": "string",
- "description": "Full product title"
- },
- "brand": {
- "type": "string",
- "description": "Product brand name"
- },
- "model": {
- "type": "string",
- "description": "Model number of the product"
- },
- "asin": {
- "type": "string",
- "description": "Amazon Standard Identification Number"
- }
- },
- "required": ["title", "brand", "model"]
- },
- "pricing": {
- "type": "object",
- "properties": {
- "currentPrice": {
- "type": "number",
- "description": "Current listed price"
- },
- "originalPrice": {
- "type": "number",
- "description": "Original price before any discounts"
- },
- "discount": {
- "type": "number",
- "description": "Discount percentage if available"
- },
- "dealAvailable": {
- "type": "boolean",
- "description": "Whether the product has an active deal"
- }
- },
- "required": ["currentPrice"]
- },
- "images": {
- "type": "array",
- "items": {
- "type": "string",
- "format": "uri",
- "description": "URL of product image"
- },
- "description": "Collection of product image URLs"
- },
- "technicalDetails": {
- "type": "object",
- "properties": {
- "voltage": {
- "type": "string",
- "description": "Battery voltage"
- },
- "torque": {
- "type": "string",
- "description": "Maximum torque rating"
- },
- "speed": {
- "type": "string",
- "description": "Speed ratings (RPM)"
- },
- "weight": {
- "type": "string",
- "description": "Weight of the tool"
- },
- "dimensions": {
- "type": "string",
- "description": "Physical dimensions"
- },
- "batteryIncluded": {
- "type": "boolean",
- "description": "Whether batteries are included"
- },
- "cordless": {
- "type": "boolean",
- "description": "Whether the tool is cordless"
- }
- }
- },
- "features": {
- "type": "array",
- "items": {
- "type": "string"
- },
- "description": "List of product features and benefits"
- },
- "categoryInfo": {
- "type": "object",
- "properties": {
- "mainCategory": {
- "type": "string",
- "description": "Main product category"
- },
- "subCategories": {
- "type": "array",
- "items": {
- "type": "string"
- },
- "description": "Sub-categories the product belongs to"
- }
- }
- },
- "reviews": {
- "type": "object",
- "properties": {
- "averageRating": {
- "type": "number",
- "minimum": 0,
- "maximum": 5,
- "description": "Average customer rating out of 5"
- },
- "numberOfReviews": {
- "type": "integer",
- "description": "Total number of customer reviews"
- },
- "ratingDistribution": {
- "type": "object",
- "properties": {
- "5star": {"type": "integer"},
- "4star": {"type": "integer"},
- "3star": {"type": "integer"},
- "2star": {"type": "integer"},
- "1star": {"type": "integer"}
- },
- "description": "Distribution of ratings by star level"
- }
- }
- },
- "availability": {
- "type": "object",
- "properties": {
- "inStock": {
- "type": "boolean",
- "description": "Whether the product is in stock"
- },
- "deliveryOptions": {
- "type": "array",
- "items": {
- "type": "string"
- },
- "description": "Available delivery options"
- },
- "estimatedDeliveryDate": {
- "type": "string",
- "description": "Estimated delivery date range"
- }
- }
- },
- "warranty": {
- "type": "string",
- "description": "Warranty information"
- },
- "compatibleAccessories": {
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "name": {"type": "string"},
- "asin": {"type": "string"},
- "url": {"type": "string", "format": "uri"}
- }
- },
- "description": "Compatible accessories for this product"
- },
- "frequentlyBoughtTogether": {
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "name": {"type": "string"},
- "asin": {"type": "string"},
- "url": {"type": "string", "format": "uri"}
- }
- },
- "description": "Products frequently bought together with this item"
- }
- },
- "required": ["productBasicInfo", "pricing", "images", "technicalDetails"]
- }
复制代码 运行:
- crwl https://a.co/d/17HZeGj -s dewalt-schema.json
复制代码
6. 指定浏览器配置文件(JSON/YAML),参数:-B,--browser-config [PATH]
- crwl https://docs.crawl4ai.com/ -B [JSON 或 YAML 格式 配置文件的路径]
复制代码- crwl https://docs.crawl4ai.com/ -b
- rowser-config [JSON 或 YAML 格式 配置文件的路径]
复制代码 指定浏览器配置文件的路径,该文件以YAML或JSON格式定义爬虫使用的浏览器举动和设置。许多网站使用 JavaScript 动态加载内容或实现反爬机制。浏览器配置文件让你能控制爬虫如何与网页交互,特别重要。
1)浏览器配置文件的主要功能
- 浏览器范例设置
- 指定使用浏览器引擎(如Chromium、Firefox)
- 设置浏览器的版本
- 请求头和身份识别
- 自定义User-Agent
- 设置HTTP头部信息
- 配置Cookie处理方式
- 渲染控制
- 等候页面完全加载的时间
- 设置JavaScript执行超时
- 控制是否加载图片和其他媒体资源
- 署理和网络设置
- 交互举动
2)例:配置文件
- browser:
- # 基本浏览器设置
- type: "chromium" # 使用Chromium浏览器
- headless: true # 无头模式(不显示浏览器界面)
-
- # 超时和等待设置
- page_load_timeout: 30000 # 页面加载超时(毫秒)
- default_wait_time: 5000 # 默认等待时间(毫秒)
-
- # 请求头设置
- user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
-
- # 媒体和资源设置
- block_images: true # 阻止加载图像
- block_css: false # 不阻止CSS
- block_fonts: true # 阻止字体加载
-
- # 代理设置
- proxy:
- server: "http://proxy-server.example.com:8080"
- username: "proxy_user"
- password: "proxy_password"
-
- # Cookie和存储设置
- cookies_enabled: true
- local_storage_enabled: true
-
- # 浏览器窗口设置
- viewport:
- width: 1920
- height: 1080
-
- # 交互行为设置
- auto_scroll:
- enabled: true
- delay: 500 # 滚动间隔(毫秒)
- scroll_amount: 200 # 每次滚动像素
- max_scrolls: 20 # 最大滚动次数
复制代码 上面有表明,打字太累,不再重复。
3)使用场景
- 有的网站使用“无限滚动”或"加载更多"按钮,需要配置自动滚动和点击举动
- 绕过反爬策略,调解User-Agent和请求频率,使爬虫举动更像真人
- 通过克制加载不需要的资源 (像:图片、字体、广告)提高爬取效率
- 使用署理网络访问,可以 DELETED 地域限制的内容
- 按照网站特点调解超时、等候时间等
- 常与 -e -c -b 共同工作
7. 指定内容过滤配置文件,参数:-f, --filter-config PATH
- crwl https://docs.crawl4ai.com/ -f [如:YAML 格式 配置文件的路径]
复制代码- crwl https://docs.crawl4ai.com/ --filter-config [如:YAML 格式 配置文件的路径]
复制代码 文件定义了如何筛选和处理爬取到的数据,屏蔽大量无关内容的网页。
1)内容过滤配置文件的主要功能
- 含与清除规则
- 基于关键词或正则表达式包罗特定内容
- 清除不相干的内容块或元素
- 设置优先级规则处理内容冲突
- 数据清算
- 去除HTML标签、广告内容
- 清算多余空缺、特别字符
- 尺度化日期、价格等格式
- 内容过滤
- 基于文本长度筛选内容
- 过滤低质量或重复内容
- 评估内容的相干性得分
- 内容分类与标志
- 将提取的内容按范例分类
- 为差别内容块添加标签
- 确定内容的层次结构
2)例:过滤配置文件
- filters:
- # 文本清理过滤器
- - name: "text_cleanup"
- type: "text_transform"
- enabled: true
- actions:
- - replace: ["[\r\n\t]+", " "] # 替换多行为单个空格
- - replace: ["\s{2,}", " "] # 替换多个空格为单个空格
- - replace: [" ", " "] # 替换HTML特殊字符
- - trim: true # 去除首尾空白
-
- # 内容包含过滤器
- - name: "content_inclusion"
- type: "inclusion"
- enabled: true
- rules:
- - field: "description"
- patterns:
- - "DEWALT"
- - "Impact Wrench"
- - "cordless"
- match_type: "any" # 匹配任一关键词即包含
-
- # 内容排除过滤器
- - name: "content_exclusion"
- type: "exclusion"
- enabled: true
- rules:
- - field: "description"
- patterns:
- - "unavailable"
- - "out of stock"
- - "advertisement"
- match_type: "any" # 匹配任一关键词即排除
-
- # 长度过滤器
- - name: "length_filter"
- type: "length"
- enabled: true
- rules:
- - field: "review"
- min_length: 50 # 最小长度(字符)
- max_length: 5000 # 最大长度(字符)
-
- # 内容分类过滤器
- - name: "content_classifier"
- type: "classifier"
- enabled: true
- rules:
- - field: "text"
- classifications:
- - name: "product_spec"
- patterns: ["specification", "technical detail", "dimension"]
- - name: "user_review"
- patterns: ["review", "rating", "stars", "purchased"]
- - name: "shipping_info"
- patterns: ["shipping", "delivery", "arrive"]
- target_field: "content_type" # 将分类结果存储到此字段
复制代码 3)使用场景
- 过滤产物形貌、规格和价格,清除广告和保举商品
- 提取有效评论,过滤掉垃圾评论和自动生成内容
- 筛选特定主题的新闻,清除不相干的广告和导航元素
- 从长篇内容中只提取所需的章节或段落
- 保留特定语言的内容,过滤其他语言
8. 使用大型语言模型(LLM)从网页中提取结构化数据,参数:-j,--json-extract TEXT
- crwl https://docs.crawl4ai.com/ -j [给LLM 的提示词 PROMPT]
复制代码- crwl https://docs.crawl4ai.com/ --json-extract [给LLM 的提示词 PROMPT]
复制代码 指定想要从爬取的网页中提取什么范例的结构化数据,而且可以通过提供形貌来引导 LLM 如何举行提取。支持的模型很多,见官网:Providers | liteLLM (https://docs.litellm.ai/docs/providers)
需要有 API KEY OF LLM
例:从网站提取信息
- crwl https://docs.crawl4ai.com/ -j “如何配置 LLM-based extraction”
复制代码
9. 把效果保存到文件里,参数: -O, --output-file PATH
- crwl https://docs.crawl4ai.com/ -O [输出结果的路径与文件名]
复制代码- crwl https://docs.crawl4ai.com/ --output-file [输出结果的路径与文件名]
复制代码
- 将效果保存到文件中,方便后续使用
- 方便将数据传递给其他步伐或工具举行后续处理
- 结合之前的 -o, --output 选项,保存为差别格式(json、markdown 等),见 CLI 第1个介绍
10. 对已爬取的网页内容提出自然语言问题,参数,-q, --question [给LLM 的提示词 PROMPT ]
- crwl https://docs.crawl4ai.com/ -q [给LLM 的提示词 PROMPT]
复制代码- crwl https://docs.crawl4ai.com/ --question [给LLM 的提示词 PROMPT]
复制代码 依靠于大型语言模型(LLM),起首 crwl 爬取并处理指定网站的内容,工具会分析爬取的内容,找到相干信息并提供答案。
例:
- crwl https://docs.crawl4ai.com/ -q "如何配置 LLM-based extraction"
复制代码
11. 指定特定的浏览器配置文件来运行,参数:-p, --profile TEXT
- crwl https://docs.crawl4ai.com/ -p TEXT
复制代码- crwl https://docs.crawl4ai.com/ --profile TEXT
复制代码 需要提前配置 Profiles 配置文件,命令:
LLM 配置方法
1. 初始设置
初次设置时,会提示输入 LLM 提供商和 API 令牌,然后将配置保存在 ~/.crawl4ai/global.yml 文件中。文件内容:
2. 使用 -e 选项,指定LLM配置文件
文件:extract_llm.yml
- type: "llm"
- provider: "openai/gpt-4"
- instruction: "提取所有文章的标题和链接"
- api_token: "token / key"
- params:
- temperature: 0.3
- max_tokens: 1000
复制代码 运行:
- crwl https://docs.crawl4ai.com/ -e extract_llm.yml
复制代码
小结:
码字太多,也未必有人看,作为自用文就写到这里。
CRAWL4AI 是一个强盛的爬虫(刮刀)软件,特别是支持 LLM 的使用。 以上只是 CLI 的功能介绍,还涉及到配置文件的示例。
在这篇文章只是详细的介绍 CLI 命令行的使用,也有详细的示例来介绍这些配置文件。但这些也只是 APP 的小部门,更多还是要去参考官方 Doc. 见文章开头的链接。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |