
# 解析函数
本文介绍User-Agent解析函数的语法规则，包括参数解释、函数示例等。
#### 函数列表
| **函数**                                                                                                                                       | **说明**                |
|:---|:---|
| [ua_parse_device] | 解析User-Agent中的设备信息。   |
| [ua_parse_os]     | 解析User-Agent中的操作系统信息。 |
| [ua_parse_agent]    | 解析User-Agent中的浏览器信息。  |
| [ua_parse_all]      | 解析User-Agent中所有信息。    |
| [url_parse]      | 解析URL的组成部分。           |
| [url_parse_qs]     | 解析URL中查询字符串包含的参数。     |
   
User-Agent解析函数会剔除解析结果为None的字段，例如解析的设备数据为{'brand': None, 'family': 'Other', 'model': None}，则brand字段和model字段将被剔除，最终的解析结果为{'family': 'Other'}。
 #### ua_parse_device
解析User-Agent中的设备信息。
- **函数格式**
  ```
  ua_parse_device(value)
  ```
  
- **参数说明**
  
  | **参数名称** | **数据类型** | **是否必填** |       **说明**       |
  |---|---|---|---|
  | value    | String   | 是        | 待解析的User-Agent字符串。 |
     
  
- **返回结果**
  返回JSON类型的数据集。
  
- **函数示例**
  - 测试数据
    ```
    {
      "http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36"
    }
    ```
    
  
  - 加工规则
    ```
    e_set("new_column",ua_parse_device(v("http_user_agent")))
    ```
    
  
  - 加工结果
    ```
    http_user_agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36
    new_column:{"family":"Mac","brand":"Apple","model":"Mac"}
    ```
    
   
 
 #### ua_parse_os
解析User-Agent中的操作系统信息。
- **函数格式**
  ```
  ua_parse_os(value)
  ```
  
- **参数说明**
  
  | **参数名称** | **数据类型** | **是否必填** |       **说明**       |
  |---|---|---|---|
  | value    | String   | 是        | 待解析的User-Agent字符串。 |
     
  
- **返回结果**
  返回JSON类型的数据集。
  
- **函数示例**
  - 测试数据
    ```
    {
        "http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36"
    }
    ```
    
  
  - 加工规则
    ```
    e_set("new_column",ua_parse_os(v("http_user_agent")))
    ```
    
  
  - 加工结果
    ```
    http_user_agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36
    new_column:{'family': 'Mac OS X', 
                          'major': '10',
                          'minor': '9', 
                          'patch': '4'}
    ```
    
   
 
 #### ua_parse_agent
解析User-Agent中的浏览器信息。
- **函数格式**
  ```
  ua_parse_agent(value)
  ```
  
- **参数说明**
  
  | **参数名称** | **数据类型** | **是否必填** |       **说明**       |
  |---|---|---|---|
  | value    | String   | 是        | 待解析的User-Agent字符串。 |
     
  
- **返回结果**
  返回JSON类型的数据集。
  
- **函数示例**
  - 测试数据
    ```
    {
       "http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36"
    }
    ```
    
  
  - 加工规则
    ```
    e_set("new_column",ua_parse_agent(v("http_user_agent")))
    ```
    
  
  - 加工结果
    ```
    http_user_agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36
    new_column:{'family': 'Chrome', 'major': '192', 'minor': '168', 'patch': '0'
    ```
    
   
 
 #### ua_parse_all
解析User-Agent中的所有信息。
- **函数格式**
  ```
  ua_parse_all(value)
  ```
  
- **参数说明**
  
  | **参数名称** | **数据类型** | **是否必填** |       **说明**       |
  |---|---|---|---|
  | value    | String   | 是        | 待解析的User-Agent字符串。 |
     
  
- **返回结果**
  返回JSON类型的数据集。
  
- **函数示例**
  - 测试数据
    ```
    {
      "http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36"
    }
    ```
    
  
  - 加工规则
    ```
    e_set("new_column",ua_parse_all(v("http_user_agent")))
    ```
    
  
  - 加工结果
    ```
    http_user_agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/192.168.0.0 Safari/537.36
    new_column: { 
     "user_agent": {
       "family": "Chrome",
        "major": "192",
        "minor": "168",
        "patch": "0"
       },
      "os": {
        "family": "Mac OS X",
        "major": "10",
        "minor": "9",
        "patch": "4"
      },
      "device": { 
       "family": "Mac",
        "brand": "Apple",
        "model": "Mac" 
     }
    }
    ```
    
   
 
 #### url_parse
解析URL的组成部分。
- **函数格式**
  ```
  url_parse(url, scheme="", allow_fragments=true)
  ```
  
- **参数说明**
  
  |    **参数名称**     | **数据类型** | **是否必填** |                                                                                                                                                                                                                                                                                                                                                                                                                     **说明**                                                                                                                                                                                                                                                                                                                                                                                                                     |
  |---|---|---|---|
  | value           | String   | 是        | 待解析的URL。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
  | scheme          | String   | 否        | 网络协议，默认为空字符。仅在URL中未指定网络协议时，返回结果中的scheme字段才会使用此处设置的值。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | allow_fragments | Boolean  | 否        | 是否解析URL中的fragment部分。 - true（默认值）：解析URL中的fragment部分，返回结果中的fragment字段为具体值。  - false：不解析URL中的fragment部分，返回结果中的fragment字段为空字符串。   |
     
  
- **返回结果**
  返回解析后的JSON数据，具体参数说明如下表所示。
  
  |  **字段**  | **说明** |
  |---|---|
  | scheme   | 网络协议   |
  | netloc   | 网络位置   |
  | path     | 分层路径标识 |
  | query    | 查询组件   |
  | fragment | 片段标识符  |
     
  
- **函数示例**
  1. 示例1：使用默认参数，返回URL的各个组成部分。
     - 测试数据
       ```
       {
          "content":"https://username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse(v("content")))
       ```
       
     
     - 加工结果
       ```
       content:https://username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib
       url:{
               "scheme": "https",
                "netloc": "username:username@example.com:8083",
                "path": "/hello/asdah/",
                "params"："type=docx",
                "query": "filename=python3.docx",
                "fragment": "urllib",
            }
       ```
       
      
  
  2. 示例2：设置allow_fragments为false，返回结果中的fragment参数值为空。
     - 测试数据
       ```
       {
          "content":"https://username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse(v("content"),allow_fragments=false))
       ```
       
     
     - 加工结果
       ```
       content:https://username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib
       url:{ 
                      "scheme": "https",
                "netloc": "username:username@example.com:8083", 
               "path": "/hello/asdah/",
                "params": "type=docx",
                "query": "filename=python3.docx", 
               "fragment": "",
            }
       ```
       
      
  
  3. 示例3：设置scheme为https，allow_fragments为false，返回结果中scheme参数值为https，fragment参数值为空。
     - 测试数据
       ```
       {
         "content":"//username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse(v("content"),scheme="https", allow_fragments=false))
       ```
       
     
     - 加工结果
       ```
       content://username:username@example.com:8083/hello/asdah/;type=docx?filename=python3.docx#urllib
       url:{ 
                "scheme": "https",
                "netloc": "username:username@example.com:8083",
                "path": "/hello/asdah/",
                "params": "type=docx",
                "query": "filename=python3.docx",
                "fragment": "",
            }
       ```
       
      
   
 
 #### url_parse_qs
解析URL中查询字符串的组成部分。
- **函数格式**
  ```
  url_parse_qs(
      url_qs,
      keep_blank_values=false,
      strict_parsing=false,
      encoding="utf-8",
      errors="replace",
      ignore_multi_fields=true,
  )
  ```
  
- **参数说明**
  
  |      **参数名称**       | **数据类型** | **是否必填** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   **说明**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
  |---|---|---|---|
  | url_qs              | String   | 是        | 待解析的URL查询字符串。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
  | keep_blank_values   | Boolean  | 否        | 是否返回值为空的参数。 - false（默认值）：不返回。  - true：返回，且将空值处理为空字符串。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | strict_parsing      | Boolean  | 否        | 是否处理解析错误。 - true：解析报错会引发ValueError异常。  - false（默认值）：忽略错误。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
  | encoding            | String   | 否        | 指定编码方式，将含有百分号（%）的转义字符解析为Unicode字符，默认为utf-8。支持ASCII。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
  | errors              | String   | 否        | 按照编码方式无法识别字符时的处理方案。取值包括： - ignore：直接忽略。  - strict：直接报错，丢弃此条日志数据。  - replace（默认值）：使用半角问号（?）替换无法识别部分。  - xmlcharrefreplace：使用对应XML字符替换无法识别部分。   |
  | ignore_multi_fields | Num      | 否        | 指定单个返回参数的值的个数。 - true（默认值）：每个参数只返回第一个值， 类型为String。  - false：每个参数都返回所有值，类型为List。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
     
  
- **返回结果**
  返回解析后的JSON数据，具体参数说明如下表所示。
  
  | **字段**  | **说明**  |
  |---|---|
  | logType | 日志类型    |
  | uid     | 日志的唯一标识 |
  | time    | 日志的时间   |
  | msg     | 日志中的信息  |
     
  
- **函数示例**
  1. 示例1：设置keep_blank_values为true，返回结果中包含值为空的参数。
     - 测试数据
       ```
       {
         "content":"logType=net_wheel_log&uid=62452****&vid=6.1.0_gf_pc&asb=1206427&git=&time=22-11-3+%e4%b8%8a11%e6%97%b649%e5%88%8633%e7%a7%92&operatingSystem=Windows+10++(10.0.0)+64bit&deviceModel=System+Product+Name+(System+manufacturer)&graphicsDeviceName=NVIDIA+GeForce+GTX+1650&graphicsDeviceType=Direct3D11&graphicsDeviceVendor=NVIDIA&graphicsDeviceVersion=Direct3D+11.0+%5blevel+11.1%5d&graphicsMemorySize=3962&systemMemorySize=8127&processorCount=6&processorFrequency=3000&processorType=Intel(R)+Core(TM)+i5-9500F+CPU+%40+3.00GHz&deviceID=96da5902a042a5f84118995f88373f73650e76be166589726****&guessUID=62452****&networkReachability=wifi&msg=GetAuthkeyRsp"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse_qs(v("content"), keep_blank_values=true))
       ```
       
     
     - 加工结果
       ```
       content:logType=net_wheel_log&uid=62452****&vid=6.1.0_gf_pc&asb=1206427&git=&time=22-11-3+%e4%b8%8a11%e6%97%b649%e5%88%8633%e7%a7%92&operatingSystem=Windows+10++(10.0.0)+64bit&deviceModel=System+Product+Name+(System+manufacturer)&graphicsDeviceName=NVIDIA+GeForce+GTX+1650&graphicsDeviceType=Direct3D11&graphicsDeviceVendor=NVIDIA&graphicsDeviceVersion=Direct3D+11.0+%5blevel+11.1%5d&graphicsMemorySize=3962&systemMemorySize=8127&processorCount=6&processorFrequency=3000&processorType=Intel(R)+Core(TM)+i5-9500F+CPU+%40+3.00GHz&deviceID=96da5902a042a5f84118995f88373f73650e76be166589726****&guessUID=62452****&networkReachability=wifi&msg=GetAuthkeyRsp
        url:{
               "logType": "net_wheel_log",
               "uid": "62452****", 
              "vid": "6.1.0_gf_pc", 
              "asb": "1206427", 
              "git": "", 
              "time": "22-11-3 上11时49分33秒",
               "operatingSystem": "Windows 10  (10.0.0) 64bit",
               "deviceModel": "System Product Name (System manufacturer)",
               "graphicsDeviceName": "NVIDIA GeForce GTX 1650",
               "graphicsDeviceType": "Direct3D11",
               "graphicsDeviceVendor": "NVIDIA",
               "graphicsDeviceVersion": "Direct3D 11.0 [level 11.1]", 
              "graphicsMemorySize": "3962",
               "systemMemorySize": "8127", 
              "processorCount": "6",
               "processorFrequency": "3000",
               "processorType": "Intel(R) Core(TM) i5-9500F CPU @ 3.00GHz", 
              "deviceID": "96da5902a042a5f84118995f88373f73650e76be166589726****", 
              "guessUID": "62452****",
              "networkReachability": "wifi", 
              "msg": "GetAuthkeyRsp",
           }
       ```
       
      
  
  2. 示例2：设置keep_blank_values为默认值（false），返回结果无值为空的参数。
     - 测试数据
       ```
       {
          "content":"logType=net_wheel_log&uid=62452****&vid=6.1.0_gf_pc&asb=1206427&git=&time=22-11-3+%e4%b8%8a11%e6%97%b649%e5%88%8633%e7%a7%92&operatingSystem=Windows+10++(10.0.0)+64bit&deviceModel=System+Product+Name+(System+manufacturer)&graphicsDeviceName=NVIDIA+GeForce+GTX+1650&graphicsDeviceType=Direct3D11&graphicsDeviceVendor=NVIDIA&graphicsDeviceVersion=Direct3D+11.0+%5blevel+11.1%5d&graphicsMemorySize=3962&systemMemorySize=8127&processorCount=6&processorFrequency=3000&processorType=Intel(R)+Core(TM)+i5-9500F+CPU+%40+3.00GHz&deviceID=96da5902a042a5f84118995f88373f73650e76be166589726****&guessUID=62452****&networkReachability=wifi&msg=GetAuthkeyRsp"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse_qs(v("content")))
       ```
       
     
     - 加工结果
       ```
       content:logType=net_wheel_log&uid=62452****&vid=6.1.0_gf_pc&asb=1206427&git=&time=22-11-3+%e4%b8%8a11%e6%97%b649%e5%88%8633%e7%a7%92&operatingSystem=Windows+10++(10.0.0)+64bit&deviceModel=System+Product+Name+(System+manufacturer)&graphicsDeviceName=NVIDIA+GeForce+GTX+1650&graphicsDeviceType=Direct3D11&graphicsDeviceVendor=NVIDIA&graphicsDeviceVersion=Direct3D+11.0+%5blevel+11.1%5d&graphicsMemorySize=3962&systemMemorySize=8127&processorCount=6&processorFrequency=3000&processorType=Intel(R)+Core(TM)+i5-9500F+CPU+%40+3.00GHz&deviceID=96da5902a042a5f84118995f88373f73650e76be166589726****&guessUID=62452****&networkReachability=wifi&msg=GetAuthkeyRsp
        url:{
               "logType": "net_wheel_log",
               "uid": "62452****",
               "vid": "6.1.0_gf_pc",
               "asb": "1206427", 
              "time": "22-11-3 上11时49分33秒",
               "operatingSystem": "Windows 10  (10.0.0) 64bit",
               "deviceModel": "System Product Name (System manufacturer)",
               "graphicsDeviceName": "NVIDIA GeForce GTX 1650",
               "graphicsDeviceType": "Direct3D11",
               "graphicsDeviceVendor": "NVIDIA", 
              "graphicsDeviceVersion": "Direct3D 11.0 [level 11.1]", 
              "graphicsMemorySize": "3962",
               "systemMemorySize": "8127", 
              "processorCount": "6", 
              "processorFrequency": "3000",
               "processorType": "Intel(R) Core(TM) i5-9500F CPU @ 3.00GHz",
               "deviceID": "96da5902a042a5f84118995f88373f73650e76be166589726****",
               "guessUID": "62452****",
               "networkReachability": "wifi",
               "msg": "GetAuthkeyRsp",
           }
       ```
       
      
  
  3. 示例3：设置ignore_multi_fields为默认值（true），每个参数只返回第一个值。
     - 测试数据
       ```
       {
          "content":"logType=net_log&uid=62452****&x=1&x=2&x=3&asb=123&asb=456"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse_qs(v("content")))
       ```
       
     
     - 加工结果
       ```
       content:logType=net_log&uid=62452****&x=1&x=2&x=3&asb=123&asb=456
       url:{
           "logType": "net_log", 
          "uid": "62452****",
           "x": "1"
           "asb": "123"
       }
       ```
       
      
  
  4. 示例4：设置ignore_multi_fields为false，每个参数返回所有值。
     - 测试数据
       ```
       {
         "content":"logType=net_log&uid=62452****&x=1&x=2&x=3&asb=123&asb=456"
       }
       ```
       
     
     - 加工规则
       ```
       e_set("url",url_parse_qs(v("content"),ignore_multi_fields=false))
       ```
       
     
     - 加工结果
       ```
       content:logType=net_log&uid=62452****&x=1&x=2&x=3&asb=123&asb=456
       url:{
           "logType": ["net_log"],
             "uid": ["62452****"],
             "x": ["1", "2", "3"],
             "asb": ["123", "456"],
       }
       ```
       
      
   
 
