< 自用文 Project-30.6 Crawl4AI > 为AI模型优化的网络爬虫工具 帮助收集 ...

打印 上一主题 下一主题

主题 1940|帖子 1940|积分 5820

官方链接:

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 
  1. pip install crawl4ai
复制代码
在 Ubuntu24 中运行:
  1. python3 -m venv -venv
  2. source venv/bin/activate
  3. pip install crawl4ai
复制代码
安装焦点库与根本依靠内容,不包罗任何高级功能。
装了一堆:



2. 自动设置

  1. crawl4ai-setup
复制代码
自动完成浏览器安装、体系兼容性检查和环境验证:
Ubuntu 24:

Windows 11:


3. 校验安装

  1. import asyncio
  2. from crawl4ai import AsyncWebCrawler, BrowserConfig, CrawlerRunConfig
  3. async def main():
  4.     async with AsyncWebCrawler() as crawler:
  5.         result = await crawler.arun(
  6.             url="https://www.google.com",
  7.         )
  8.         print(result.markdown[:300])  # Show the first 300 characters of extracted text
  9. if __name__ == "__main__":
  10.     asyncio.run(main())
复制代码
正常是:


4. 安装诊断命令

  1. crawl4ai-doctor
复制代码


使用 Crawl4AI

根本的 CLI 方式:

1. 指定格式输出,参数:-o 或 --output [all|json|markdown|md|markdown-fit|md-fit]

  1. crwl https://docs.crawl4ai.com/ -o markdown
复制代码

2. 不使用已经存在的缓存,从服务器获取最新内容,参数: -b , --bypass-cache

  1. crwl https://docs.crawl4ai.com/ --bypass-cache
复制代码
  1. crwl https://docs.crawl4ai.com/ -b
复制代码

3. 带有详细的运行信息和日志, 参数: -v, --verbose

详细信息:


  • 爬取的每个步骤
  • 请求和响应的详情
  • 遇到的问题或警告
  • 处理过程中的状态变化
  • 性能相干的信息(如加载时间)
对于调试问题或相识步伐的工作方式特别有效。如网站爬取失败或效果不符合预期,verbose 模式可以帮助你确定问题出在那里。
  1. crwl https://docs.crawl4ai.com/ -v
复制代码
  1. crwl https://docs.crawl4ai.com/ --verbose
复制代码

4. 指定提取策略的文件 参数: -e, --extraction-config [PATH]

  1. crwl https://docs.crawl4ai.com/ -e [策略文件路径]
复制代码
  1. crwl https://docs.crawl4ai.com/ --extraction-config [策略文件路径]
复制代码
自定义爬虫如何从网页中提取内容的详细规则和策略,可以准确控制数据提取,使用YAML或JSON格式结构。
1) 提取策略配置文件的主要功能



  • 指定使用哪种范例的选择器(CSS、XPath、正则表达式等)来定位页面元素
  • 定义如那边理和转换提取的原始内容

    • 删除多余空格、HTML标签
    • 将价格文本转为数字
    • 内容分割和合并

  • 支持嵌套数据结构的提取

    • 先定位父元素,再从父元素中提取子元素
    • 处理列表、表格等复杂结构

  • 指定条件,决定是否提取内容

    • 如:只提取含有特定关键词的段落

  • 定义在多页面间导航以提取内容

    • 分页处理

2) 例:从Amazon产物页面提取DEWALT电动工具的各种信息

主要提取内容



  • 根本信息:标题、品牌、型号、ASIN
  • 价格信息:当前价格、原价、优惠状态
  • 产物图片:主图及全部备选图片
  • 技术规格:电压、扭矩、重量、尺寸等
  • 产物特点:全部列出的产物特性
  • 评论数据:平均评分和评论数目
  • 可用性:库存状态和配送选项
  • 分类信息:主类别和面包屑导航中的子类别
  • 相干产物:经常一起购买的商品
文件名:C:\temp\dewalt-extraction-strategy.yaml
  1. extraction_strategies:
  2.   # Basic product information
  3.   - name: "product_title"
  4.     selector_type: "css"
  5.     selector: "#productTitle"
  6.     attribute: "text"
  7.     transform: "trim"
  8.     target_field: "productBasicInfo.title"
  9.   
  10.   - name: "product_brand"
  11.     selector_type: "css"
  12.     selector: "#bylineInfo"
  13.     attribute: "text"
  14.     transforms:
  15.       - "trim"
  16.       - "regex_replace:Brand: (.*)|Visit the (.*) Store|(.*) Store|$1$2$3"
  17.     target_field: "productBasicInfo.brand"
  18.   
  19.   - name: "product_model"
  20.     selector_type: "css"
  21.     selector: "tr.po-model_name td.po-model_name"
  22.     attribute: "text"
  23.     transform: "trim"
  24.     target_field: "productBasicInfo.model"
  25.   
  26.   - name: "product_asin"
  27.     selector_type: "css"
  28.     selector: "tr:contains('ASIN') td.a-span9"
  29.     attribute: "text"
  30.     transform: "trim"
  31.     target_field: "productBasicInfo.asin"
  32.   
  33.   # Price information
  34.   - name: "current_price"
  35.     selector_type: "css"
  36.     selector: ".priceToPay .a-offscreen"
  37.     attribute: "text"
  38.     transforms:
  39.       - "trim"
  40.       - "regex_replace:\\$(.*)$|$1"
  41.       - "to_number"
  42.     target_field: "pricing.currentPrice"
  43.   
  44.   - name: "original_price"
  45.     selector_type: "css"
  46.     selector: ".basisPrice .a-offscreen"
  47.     attribute: "text"
  48.     transforms:
  49.       - "trim"
  50.       - "regex_replace:\\$(.*)$|$1"
  51.       - "to_number"
  52.     target_field: "pricing.originalPrice"
  53.   
  54.   - name: "deal_badge"
  55.     selector_type: "css"
  56.     selector: "#dealBadge"
  57.     exists_as: "dealAvailable"
  58.     target_field: "pricing.dealAvailable"
  59.   
  60.   # Images
  61.   - name: "product_images"
  62.     selector_type: "css"
  63.     selector: "#landingImage"
  64.     attribute: "data-old-hires"
  65.     target_field: "images"
  66.     is_list: true
  67.   
  68.   - name: "alternate_images"
  69.     selector_type: "css"
  70.     selector: "#altImages .item img"
  71.     attribute: "src"
  72.     transforms:
  73.       - "regex_replace:(.*)\\._.*\\.jpg$|$1.jpg"
  74.     target_field: "images"
  75.     append: true
  76.     is_list: true
  77.   
  78.   # Technical details
  79.   - name: "voltage"
  80.     selector_type: "css"
  81.     selector: "tr:contains('Voltage') td.a-span9"
  82.     attribute: "text"
  83.     transform: "trim"
  84.     target_field: "technicalDetails.voltage"
  85.   
  86.   - name: "torque"
  87.     selector_type: "css"
  88.     selector: "tr:contains('Torque') td.a-span9, tr:contains('Maximum Torque') td.a-span9"
  89.     attribute: "text"
  90.     transform: "trim"
  91.     target_field: "technicalDetails.torque"
  92.   
  93.   - name: "weight"
  94.     selector_type: "css"
  95.     selector: "tr:contains('Item Weight') td.a-span9"
  96.     attribute: "text"
  97.     transform: "trim"
  98.     target_field: "technicalDetails.weight"
  99.   
  100.   - name: "dimensions"
  101.     selector_type: "css"
  102.     selector: "tr:contains('Product Dimensions') td.a-span9"
  103.     attribute: "text"
  104.     transform: "trim"
  105.     target_field: "technicalDetails.dimensions"
  106.   
  107.   - name: "battery_included"
  108.     selector_type: "css"
  109.     selector: "tr:contains('Batteries Included') td.a-span9"
  110.     attribute: "text"
  111.     transforms:
  112.       - "trim"
  113.       - "to_boolean:Yes=true,No=false"
  114.     target_field: "technicalDetails.batteryIncluded"
  115.   
  116.   - name: "cordless"
  117.     selector_type: "css"
  118.     selector: "tr:contains('Power Source') td.a-span9"
  119.     attribute: "text"
  120.     transforms:
  121.       - "trim"
  122.       - "to_boolean:Battery Powered=true,*=false"
  123.     target_field: "technicalDetails.cordless"
  124.   
  125.   # Features
  126.   - name: "features"
  127.     selector_type: "css"
  128.     selector: "#feature-bullets .a-list-item"
  129.     attribute: "text"
  130.     transform: "trim"
  131.     target_field: "features"
  132.     is_list: true
  133.   
  134.   # Reviews
  135.   - name: "average_rating"
  136.     selector_type: "css"
  137.     selector: "#acrPopover .a-declarative"
  138.     attribute: "title"
  139.     transforms:
  140.       - "regex_replace:(.*) out of 5 stars|$1"
  141.       - "to_number"
  142.     target_field: "reviews.averageRating"
  143.   
  144.   - name: "number_of_reviews"
  145.     selector_type: "css"
  146.     selector: "#acrCustomerReviewText"
  147.     attribute: "text"
  148.     transforms:
  149.       - "regex_replace:([0-9,]+) ratings|$1"
  150.       - "regex_replace:,||g"
  151.       - "to_number"
  152.     target_field: "reviews.numberOfReviews"
  153.   
  154.   # Availability
  155.   - name: "in_stock"
  156.     selector_type: "css"
  157.     selector: "#availability"
  158.     attribute: "text"
  159.     transforms:
  160.       - "trim"
  161.       - "to_boolean:In Stock=true,*=false"
  162.     target_field: "availability.inStock"
  163.   
  164.   - name: "delivery_options"
  165.     selector_type: "css"
  166.     selector: "#deliveryBlockMessage .a-list-item"
  167.     attribute: "text"
  168.     transform: "trim"
  169.     target_field: "availability.deliveryOptions"
  170.     is_list: true
  171.   
  172.   # Warranty
  173.   - name: "warranty"
  174.     selector_type: "css"
  175.     selector: "tr:contains('Warranty Description') td.a-span9"
  176.     attribute: "text"
  177.     transform: "trim"
  178.     target_field: "warranty"
  179.   
  180.   # Category info
  181.   - name: "main_category"
  182.     selector_type: "css"
  183.     selector: "#wayfinding-breadcrumbs_feature_div li:last-child"
  184.     attribute: "text"
  185.     transform: "trim"
  186.     target_field: "categoryInfo.mainCategory"
  187.   
  188.   - name: "sub_categories"
  189.     selector_type: "css"
  190.     selector: "#wayfinding-breadcrumbs_feature_div li:not(:last-child)"
  191.     attribute: "text"
  192.     transform: "trim"
  193.     target_field: "categoryInfo.subCategories"
  194.     is_list: true
  195.   
  196.   # Frequently bought together
  197.   - name: "frequently_bought_together"
  198.     selector_type: "css"
  199.     selector: "#sims-consolidated-2_feature_div .a-carousel-card h2"
  200.     attribute: "text"
  201.     transform: "trim"
  202.     target_field: "frequentlyBoughtTogether.name"
  203.     is_list: true
  204.    
  205.   - name: "frequently_bought_together_links"
  206.     selector_type: "css"
  207.     selector: "#sims-consolidated-2_feature_div .a-carousel-card a"
  208.     attribute: "href"
  209.     transform: "trim"
  210.     target_field: "frequentlyBoughtTogether.url"
  211.     is_list: true
复制代码
运行:
  1. crwl https://a.co/d/17HZeGj -e dewalt-extraction-strategy.yaml -s dewalt-schema.json
复制代码
由于:
大概是太频繁,或我的 VPN 有在黑名单,没抓到数据。


5. 指定一个JSON模式(JSON schema)文件的路径 参数:-s, --schema [PATH]

  1. crwl https://docs.crawl4ai.com/ -s [JSON schema 文件的路径]
复制代码
  1. 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
  1. {
  2.   "$schema": "http://json-schema.org/draft-07/schema#",
  3.   "title": "Amazon Power Tool Product Schema",
  4.   "description": "Schema for extracting DEWALT impact wrench product details from Amazon",
  5.   "type": "object",
  6.   "properties": {
  7.     "productBasicInfo": {
  8.       "type": "object",
  9.       "properties": {
  10.         "title": {
  11.           "type": "string",
  12.           "description": "Full product title"
  13.         },
  14.         "brand": {
  15.           "type": "string",
  16.           "description": "Product brand name"
  17.         },
  18.         "model": {
  19.           "type": "string",
  20.           "description": "Model number of the product"
  21.         },
  22.         "asin": {
  23.           "type": "string",
  24.           "description": "Amazon Standard Identification Number"
  25.         }
  26.       },
  27.       "required": ["title", "brand", "model"]
  28.     },
  29.     "pricing": {
  30.       "type": "object",
  31.       "properties": {
  32.         "currentPrice": {
  33.           "type": "number",
  34.           "description": "Current listed price"
  35.         },
  36.         "originalPrice": {
  37.           "type": "number",
  38.           "description": "Original price before any discounts"
  39.         },
  40.         "discount": {
  41.           "type": "number",
  42.           "description": "Discount percentage if available"
  43.         },
  44.         "dealAvailable": {
  45.           "type": "boolean",
  46.           "description": "Whether the product has an active deal"
  47.         }
  48.       },
  49.       "required": ["currentPrice"]
  50.     },
  51.     "images": {
  52.       "type": "array",
  53.       "items": {
  54.         "type": "string",
  55.         "format": "uri",
  56.         "description": "URL of product image"
  57.       },
  58.       "description": "Collection of product image URLs"
  59.     },
  60.     "technicalDetails": {
  61.       "type": "object",
  62.       "properties": {
  63.         "voltage": {
  64.           "type": "string",
  65.           "description": "Battery voltage"
  66.         },
  67.         "torque": {
  68.           "type": "string",
  69.           "description": "Maximum torque rating"
  70.         },
  71.         "speed": {
  72.           "type": "string",
  73.           "description": "Speed ratings (RPM)"
  74.         },
  75.         "weight": {
  76.           "type": "string",
  77.           "description": "Weight of the tool"
  78.         },
  79.         "dimensions": {
  80.           "type": "string",
  81.           "description": "Physical dimensions"
  82.         },
  83.         "batteryIncluded": {
  84.           "type": "boolean",
  85.           "description": "Whether batteries are included"
  86.         },
  87.         "cordless": {
  88.           "type": "boolean",
  89.           "description": "Whether the tool is cordless"
  90.         }
  91.       }
  92.     },
  93.     "features": {
  94.       "type": "array",
  95.       "items": {
  96.         "type": "string"
  97.       },
  98.       "description": "List of product features and benefits"
  99.     },
  100.     "categoryInfo": {
  101.       "type": "object",
  102.       "properties": {
  103.         "mainCategory": {
  104.           "type": "string",
  105.           "description": "Main product category"
  106.         },
  107.         "subCategories": {
  108.           "type": "array",
  109.           "items": {
  110.             "type": "string"
  111.           },
  112.           "description": "Sub-categories the product belongs to"
  113.         }
  114.       }
  115.     },
  116.     "reviews": {
  117.       "type": "object",
  118.       "properties": {
  119.         "averageRating": {
  120.           "type": "number",
  121.           "minimum": 0,
  122.           "maximum": 5,
  123.           "description": "Average customer rating out of 5"
  124.         },
  125.         "numberOfReviews": {
  126.           "type": "integer",
  127.           "description": "Total number of customer reviews"
  128.         },
  129.         "ratingDistribution": {
  130.           "type": "object",
  131.           "properties": {
  132.             "5star": {"type": "integer"},
  133.             "4star": {"type": "integer"},
  134.             "3star": {"type": "integer"},
  135.             "2star": {"type": "integer"},
  136.             "1star": {"type": "integer"}
  137.           },
  138.           "description": "Distribution of ratings by star level"
  139.         }
  140.       }
  141.     },
  142.     "availability": {
  143.       "type": "object",
  144.       "properties": {
  145.         "inStock": {
  146.           "type": "boolean",
  147.           "description": "Whether the product is in stock"
  148.         },
  149.         "deliveryOptions": {
  150.           "type": "array",
  151.           "items": {
  152.             "type": "string"
  153.           },
  154.           "description": "Available delivery options"
  155.         },
  156.         "estimatedDeliveryDate": {
  157.           "type": "string",
  158.           "description": "Estimated delivery date range"
  159.         }
  160.       }
  161.     },
  162.     "warranty": {
  163.       "type": "string",
  164.       "description": "Warranty information"
  165.     },
  166.     "compatibleAccessories": {
  167.       "type": "array",
  168.       "items": {
  169.         "type": "object",
  170.         "properties": {
  171.           "name": {"type": "string"},
  172.           "asin": {"type": "string"},
  173.           "url": {"type": "string", "format": "uri"}
  174.         }
  175.       },
  176.       "description": "Compatible accessories for this product"
  177.     },
  178.     "frequentlyBoughtTogether": {
  179.       "type": "array",
  180.       "items": {
  181.         "type": "object",
  182.         "properties": {
  183.           "name": {"type": "string"},
  184.           "asin": {"type": "string"},
  185.           "url": {"type": "string", "format": "uri"}
  186.         }
  187.       },
  188.       "description": "Products frequently bought together with this item"
  189.     }
  190.   },
  191.   "required": ["productBasicInfo", "pricing", "images", "technicalDetails"]
  192. }
复制代码
运行:
  1. crwl https://a.co/d/17HZeGj -s dewalt-schema.json
复制代码


6. 指定浏览器配置文件(JSON/YAML),参数:-B,--browser-config [PATH]

  1. crwl https://docs.crawl4ai.com/ -B [JSON 或 YAML 格式 配置文件的路径]
复制代码
  1. crwl https://docs.crawl4ai.com/ -b
  2. rowser-config [JSON 或 YAML 格式 配置文件的路径]
复制代码
指定浏览器配置文件的路径,该文件以YAML或JSON格式定义爬虫使用的浏览器举动和设置。许多网站使用 JavaScript 动态加载内容或实现反爬机制。浏览器配置文件让你能控制爬虫如何与网页交互,特别重要。
1)浏览器配置文件的主要功能



  • 浏览器范例设置

    • 指定使用浏览器引擎(如Chromium、Firefox)
    • 设置浏览器的版本

  • 请求头和身份识别

    • 自定义User-Agent
    • 设置HTTP头部信息
    • 配置Cookie处理方式

  • 渲染控制

    • 等候页面完全加载的时间
    • 设置JavaScript执行超时
    • 控制是否加载图片和其他媒体资源

  • 署理和网络设置

    • 配置署理服务器
    • 设置网络请求超时
    • 控制并发连接数

  • 交互举动

    • 自动滚动页面
    • 模拟鼠标移动
    • 配置点击举动

2)例:配置文件

  1. browser:
  2.   # 基本浏览器设置
  3.   type: "chromium"  # 使用Chromium浏览器
  4.   headless: true    # 无头模式(不显示浏览器界面)
  5.   
  6.   # 超时和等待设置
  7.   page_load_timeout: 30000   # 页面加载超时(毫秒)
  8.   default_wait_time: 5000    # 默认等待时间(毫秒)
  9.   
  10.   # 请求头设置
  11.   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"
  12.   
  13.   # 媒体和资源设置
  14.   block_images: true          # 阻止加载图像
  15.   block_css: false            # 不阻止CSS
  16.   block_fonts: true           # 阻止字体加载
  17.   
  18.   # 代理设置
  19.   proxy:
  20.     server: "http://proxy-server.example.com:8080"
  21.     username: "proxy_user"
  22.     password: "proxy_password"
  23.   
  24.   # Cookie和存储设置
  25.   cookies_enabled: true
  26.   local_storage_enabled: true
  27.   
  28.   # 浏览器窗口设置
  29.   viewport:
  30.     width: 1920
  31.     height: 1080
  32.    
  33.   # 交互行为设置
  34.   auto_scroll:
  35.     enabled: true
  36.     delay: 500          # 滚动间隔(毫秒)
  37.     scroll_amount: 200  # 每次滚动像素
  38.     max_scrolls: 20     # 最大滚动次数
复制代码
上面有表明,打字太累,不再重复。
3)使用场景



  • 有的网站使用“无限滚动”或"加载更多"按钮,需要配置自动滚动和点击举动
  • 绕过反爬策略,调解User-Agent和请求频率,使爬虫举动更像真人
  • 通过克制加载不需要的资源 (像:图片、字体、广告)提高爬取效率
  • 使用署理网络访问,可以 DELETED 地域限制的内容
  • 按照网站特点调解超时、等候时间等
  • 常与 -e -c -b 共同工作

7. 指定内容过滤配置文件,参数:-f, --filter-config PATH

  1. crwl https://docs.crawl4ai.com/ -f [如:YAML 格式 配置文件的路径]
复制代码
  1. crwl https://docs.crawl4ai.com/ --filter-config [如:YAML 格式 配置文件的路径]
复制代码
文件定义了如何筛选和处理爬取到的数据,屏蔽大量无关内容的网页。
1)内容过滤配置文件的主要功能



  • 含与清除规则

    • 基于关键词或正则表达式包罗特定内容
    • 清除不相干的内容块或元素
    • 设置优先级规则处理内容冲突

  • 数据清算

    • 去除HTML标签、广告内容
    • 清算多余空缺、特别字符
    • 尺度化日期、价格等格式

  • 内容过滤

    • 基于文本长度筛选内容
    • 过滤低质量或重复内容
    • 评估内容的相干性得分

  • 内容分类与标志

    • 将提取的内容按范例分类
    • 为差别内容块添加标签
    • 确定内容的层次结构

2)例:过滤配置文件

  1. filters:
  2.   # 文本清理过滤器
  3.   - name: "text_cleanup"
  4.     type: "text_transform"
  5.     enabled: true
  6.     actions:
  7.       - replace: ["[\r\n\t]+", " "]        # 替换多行为单个空格
  8.       - replace: ["\s{2,}", " "]           # 替换多个空格为单个空格
  9.       - replace: [" ", " "]           # 替换HTML特殊字符
  10.       - trim: true                          # 去除首尾空白
  11.   
  12.   # 内容包含过滤器
  13.   - name: "content_inclusion"
  14.     type: "inclusion"
  15.     enabled: true
  16.     rules:
  17.       - field: "description"
  18.         patterns:
  19.           - "DEWALT"
  20.           - "Impact Wrench"
  21.           - "cordless"
  22.         match_type: "any"         # 匹配任一关键词即包含
  23.   
  24.   # 内容排除过滤器
  25.   - name: "content_exclusion"
  26.     type: "exclusion"
  27.     enabled: true
  28.     rules:
  29.       - field: "description"
  30.         patterns:
  31.           - "unavailable"
  32.           - "out of stock"
  33.           - "advertisement"
  34.         match_type: "any"         # 匹配任一关键词即排除
  35.   
  36.   # 长度过滤器
  37.   - name: "length_filter"
  38.     type: "length"
  39.     enabled: true
  40.     rules:
  41.       - field: "review"
  42.         min_length: 50            # 最小长度(字符)
  43.         max_length: 5000          # 最大长度(字符)
  44.   
  45.   # 内容分类过滤器
  46.   - name: "content_classifier"
  47.     type: "classifier"
  48.     enabled: true
  49.     rules:
  50.       - field: "text"
  51.         classifications:
  52.           - name: "product_spec"
  53.             patterns: ["specification", "technical detail", "dimension"]
  54.           - name: "user_review"
  55.             patterns: ["review", "rating", "stars", "purchased"]
  56.           - name: "shipping_info"
  57.             patterns: ["shipping", "delivery", "arrive"]
  58.         target_field: "content_type"  # 将分类结果存储到此字段
复制代码
3)使用场景



  • 过滤产物形貌、规格和价格,清除广告和保举商品
  • 提取有效评论,过滤掉垃圾评论和自动生成内容
  • 筛选特定主题的新闻,清除不相干的广告和导航元素
  • 从长篇内容中只提取所需的章节或段落
  • 保留特定语言的内容,过滤其他语言

8. 使用大型语言模型(LLM)从网页中提取结构化数据,参数:-j,--json-extract TEXT

  1. crwl https://docs.crawl4ai.com/ -j [给LLM 的提示词 PROMPT]
复制代码
  1. crwl https://docs.crawl4ai.com/ --json-extract [给LLM 的提示词 PROMPT]
复制代码
指定想要从爬取的网页中提取什么范例的结构化数据,而且可以通过提供形貌来引导 LLM 如何举行提取。支持的模型很多,见官网:Providers | liteLLM (https://docs.litellm.ai/docs/providers) 
需要有 API KEY OF LLM
例:从网站提取信息
  1. crwl https://docs.crawl4ai.com/ -j “如何配置 LLM-based extraction”
复制代码



9. 把效果保存到文件里,参数: -O, --output-file PATH 

  1. crwl https://docs.crawl4ai.com/ -O [输出结果的路径与文件名]
复制代码
  1. crwl https://docs.crawl4ai.com/ --output-file [输出结果的路径与文件名]
复制代码


  • 将效果保存到文件中,方便后续使用
  • 方便将数据传递给其他步伐或工具举行后续处理
  •  结合之前的 -o, --output 选项,保存为差别格式(json、markdown 等),见 CLI 第1个介绍

10. 对已爬取的网页内容提出自然语言问题,参数,-q, --question [给LLM 的提示词 PROMPT ]

  1. crwl https://docs.crawl4ai.com/ -q [给LLM 的提示词 PROMPT]
复制代码
  1. crwl https://docs.crawl4ai.com/ --question [给LLM 的提示词 PROMPT]
复制代码
依靠于大型语言模型(LLM),起首 crwl 爬取并处理指定网站的内容,工具会分析爬取的内容,找到相干信息并提供答案。
例:

  1. crwl https://docs.crawl4ai.com/ -q "如何配置 LLM-based extraction"
复制代码


11. 指定特定的浏览器配置文件来运行,参数:-p, --profile TEXT

  1. crwl https://docs.crawl4ai.com/ -p TEXT
复制代码
  1. crwl https://docs.crawl4ai.com/ --profile TEXT
复制代码
需要提前配置 Profiles 配置文件,命令:
  1. crwl profiles
复制代码


LLM 配置方法

1. 初始设置

初次设置时,会提示输入 LLM 提供商和 API 令牌,然后将配置保存在 ~/.crawl4ai/global.yml 文件中。文件内容:



2. 使用 -e 选项,指定LLM配置文件

文件:extract_llm.yml
  1. type: "llm"
  2. provider: "openai/gpt-4"
  3. instruction: "提取所有文章的标题和链接"
  4. api_token: "token / key"
  5. params:
  6.   temperature: 0.3
  7.   max_tokens: 1000
复制代码
运行:
  1. crwl https://docs.crawl4ai.com/  -e extract_llm.yml
复制代码



小结:

码字太多,也未必有人看,作为自用文就写到这里。
CRAWL4AI 是一个强盛的爬虫(刮刀)软件,特别是支持 LLM 的使用。 以上只是 CLI 的功能介绍,还涉及到配置文件的示例。
在这篇文章只是详细的介绍 CLI 命令行的使用,也有详细的示例来介绍这些配置文件。但这些也只是 APP 的小部门,更多还是要去参考官方 Doc.  见文章开头的链接。










免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表