美文网首页Python
抓包导出的har格式解析

抓包导出的har格式解析

作者: 萌萌聊测试 | 来源:发表于2022-05-16 21:35 被阅读0次

    使用Chrome开发者工具即Charles等抓包后可以到处.har格式的网络请求包,例如:

    1、打开Chrome浏览器

    2、打开开发者工具,切换到网络面板

    3、浏览器访问https://httpbin.org/get?a=1&b=2

    4、在开发者工具,网络面包点击导出图标,如下图:

    image.png

    选择位置并保持,得到一个httpbin.org.har的文件。

    这个文件实际上是一个基于JSON格式的纯文本文件,可以使用记事本等打开。例如使用VsCode打开后,显示如下:

    image.png

    完整格式如下:

    {
      "log": {
        "version": "1.2",
        "creator": {
          "name": "WebInspector",
          "version": "537.36"
        },
        "pages": [
          {
            "startedDateTime": "2022-05-12T01:45:27.174Z",
            "id": "page_1",
            "title": "https://httpbin.org/get?a=1&b=2",
            "pageTimings": {
              "onContentLoad": 1118.679000006523,
              "onLoad": 1379.2790000443347
            }
          }
        ],
        "entries": [
          {
            "_initiator": {
              "type": "other"
            },
            "_priority": "VeryHigh",
            "_resourceType": "document",
            "cache": {},
            "connection": "396",
            "pageref": "page_1",
            "request": {
              "method": "GET",
              "url": "https://httpbin.org/get?a=1&b=2",
              "httpVersion": "http/2.0",
              "headers": [
                {
                  "name": ":authority",
                  "value": "httpbin.org"
                },
                {
                  "name": ":method",
                  "value": "GET"
                },
                {
                  "name": ":path",
                  "value": "/get?a=1&b=2"
                },
                {
                  "name": ":scheme",
                  "value": "https"
                },
                {
                  "name": "accept",
                  "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
                },
                {
                  "name": "accept-encoding",
                  "value": "gzip, deflate, br"
                },
                {
                  "name": "accept-language",
                  "value": "zh-CN,zh;q=0.9,en;q=0.8"
                },
                {
                  "name": "cache-control",
                  "value": "no-cache"
                },
                {
                  "name": "dnt",
                  "value": "1"
                },
                {
                  "name": "pragma",
                  "value": "no-cache"
                },
                {
                  "name": "sec-ch-ua",
                  "value": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\""
                },
                {
                  "name": "sec-ch-ua-mobile",
                  "value": "?0"
                },
                {
                  "name": "sec-ch-ua-platform",
                  "value": "\"macOS\""
                },
                {
                  "name": "sec-fetch-dest",
                  "value": "document"
                },
                {
                  "name": "sec-fetch-mode",
                  "value": "navigate"
                },
                {
                  "name": "sec-fetch-site",
                  "value": "none"
                },
                {
                  "name": "sec-fetch-user",
                  "value": "?1"
                },
                {
                  "name": "upgrade-insecure-requests",
                  "value": "1"
                },
                {
                  "name": "user-agent",
                  "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36"
                }
              ],
              "queryString": [
                {
                  "name": "a",
                  "value": "1"
                },
                {
                  "name": "b",
                  "value": "2"
                }
              ],
              "cookies": [],
              "headersSize": -1,
              "bodySize": 0
            },
            "response": {
              "status": 200,
              "statusText": "",
              "httpVersion": "http/2.0",
              "headers": [
                {
                  "name": "access-control-allow-credentials",
                  "value": "true"
                },
                {
                  "name": "access-control-allow-origin",
                  "value": "*"
                },
                {
                  "name": "content-length",
                  "value": "1052"
                },
                {
                  "name": "content-type",
                  "value": "application/json"
                },
                {
                  "name": "date",
                  "value": "Thu, 12 May 2022 01:45:12 GMT"
                },
                {
                  "name": "server",
                  "value": "gunicorn/19.9.0"
                }
              ],
              "cookies": [],
              "content": {
                "size": 1052,
                "mimeType": "application/json",
                "text": "{\n  \"args\": {\n    \"a\": \"1\", \n    \"b\": \"2\"\n  }, \n  \"headers\": {\n    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n    \"Accept-Encoding\": \"gzip, deflate, br\", \n    \"Accept-Language\": \"zh-CN,zh;q=0.9,en;q=0.8\", \n    \"Cache-Control\": \"no-cache\", \n    \"Dnt\": \"1\", \n    \"Host\": \"httpbin.org\", \n    \"Pragma\": \"no-cache\", \n    \"Sec-Ch-Ua\": \"\\\" Not A;Brand\\\";v=\\\"99\\\", \\\"Chromium\\\";v=\\\"101\\\", \\\"Google Chrome\\\";v=\\\"101\\\"\", \n    \"Sec-Ch-Ua-Mobile\": \"?0\", \n    \"Sec-Ch-Ua-Platform\": \"\\\"macOS\\\"\", \n    \"Sec-Fetch-Dest\": \"document\", \n    \"Sec-Fetch-Mode\": \"navigate\", \n    \"Sec-Fetch-Site\": \"none\", \n    \"Sec-Fetch-User\": \"?1\", \n    \"Upgrade-Insecure-Requests\": \"1\", \n    \"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36\", \n    \"X-Amzn-Trace-Id\": \"Root=1-627c66a8-2b7fa5721cd3296b14fb6362\"\n  }, \n  \"origin\": \"114.247.113.179\", \n  \"url\": \"https://httpbin.org/get?a=1&b=2\"\n}\n"
              },
              "redirectURL": "",
              "headersSize": -1,
              "bodySize": -1,
              "_transferSize": 1194,
              "_error": null
            },
            "serverIPAddress": "44.195.242.112",
            "startedDateTime": "2022-05-12T01:45:27.172Z",
            "time": 1051.2589999805539,
            "timings": {
              "blocked": 27.15599998584017,
              "dns": 0.013999999999999346,
              "ssl": 462.058,
              "connect": 462.686,
              "send": 0.6610000000000014,
              "wait": 560.4049999897666,
              "receive": 0.3370000049471855,
              "_blocked_queueing": 1.8709999858401716
            }
          },
          {
            "_initiator": {
              "type": "other"
            },
            "_priority": "High",
            "_resourceType": "other",
            "cache": {},
            "connection": "396",
            "pageref": "page_1",
            "request": {
              "method": "GET",
              "url": "https://httpbin.org/favicon.ico",
              "httpVersion": "http/2.0",
              "headers": [
                {
                  "name": ":authority",
                  "value": "httpbin.org"
                },
                {
                  "name": ":method",
                  "value": "GET"
                },
                {
                  "name": ":path",
                  "value": "/favicon.ico"
                },
                {
                  "name": ":scheme",
                  "value": "https"
                },
                {
                  "name": "accept",
                  "value": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"
                },
                {
                  "name": "accept-encoding",
                  "value": "gzip, deflate, br"
                },
                {
                  "name": "accept-language",
                  "value": "zh-CN,zh;q=0.9,en;q=0.8"
                },
                {
                  "name": "cache-control",
                  "value": "no-cache"
                },
                {
                  "name": "cookie",
                  "value": "_dd_s=logs=1&id=89afa2ae-542e-466c-91e3-690fda08e5c8&created=1652319928481&expire=1652320828481"
                },
                {
                  "name": "dnt",
                  "value": "1"
                },
                {
                  "name": "pragma",
                  "value": "no-cache"
                },
                {
                  "name": "referer",
                  "value": "https://httpbin.org/get?a=1&b=2"
                },
                {
                  "name": "sec-ch-ua",
                  "value": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\""
                },
                {
                  "name": "sec-ch-ua-mobile",
                  "value": "?0"
                },
                {
                  "name": "sec-ch-ua-platform",
                  "value": "\"macOS\""
                },
                {
                  "name": "sec-fetch-dest",
                  "value": "image"
                },
                {
                  "name": "sec-fetch-mode",
                  "value": "no-cors"
                },
                {
                  "name": "sec-fetch-site",
                  "value": "same-origin"
                },
                {
                  "name": "user-agent",
                  "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36"
                }
              ],
              "queryString": [],
              "cookies": [
                {
                  "name": "_dd_s",
                  "value": "logs=1&id=89afa2ae-542e-466c-91e3-690fda08e5c8&created=1652319928481&expire=1652320828481",
                  "path": "/",
                  "domain": "httpbin.org",
                  "expires": "2022-05-12T02:00:28.000Z",
                  "httpOnly": false,
                  "secure": false,
                  "sameSite": "Strict"
                }
              ],
              "headersSize": -1,
              "bodySize": 0
            },
            "response": {
              "status": 404,
              "statusText": "",
              "httpVersion": "http/2.0",
              "headers": [
                {
                  "name": "access-control-allow-credentials",
                  "value": "true"
                },
                {
                  "name": "access-control-allow-origin",
                  "value": "*"
                },
                {
                  "name": "content-length",
                  "value": "233"
                },
                {
                  "name": "content-type",
                  "value": "text/html"
                },
                {
                  "name": "date",
                  "value": "Thu, 12 May 2022 01:45:13 GMT"
                },
                {
                  "name": "server",
                  "value": "gunicorn/19.9.0"
                }
              ],
              "cookies": [],
              "content": {
                "size": 233,
                "mimeType": "text/html",
                "text": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>\n"
              },
              "redirectURL": "",
              "headersSize": -1,
              "bodySize": -1,
              "_transferSize": 369,
              "_error": null
            },
            "serverIPAddress": "44.195.242.112",
            "startedDateTime": "2022-05-12T01:45:28.562Z",
            "time": 229.52200000872836,
            "timings": {
              "blocked": 1.5150000325813888,
              "dns": -1,
              "ssl": -1,
              "connect": -1,
              "send": 0.19500000000000006,
              "wait": 227.42900002838672,
              "receive": 0.38299994776025414,
              "_blocked_queueing": 0.784000032581389
            }
          }
        ]
      }
    }
    

    其基本格式如下:

    log: 捕获的纪录,最外层

    pages: 捕获的页面,每个页面有个页面id

    entries: 捕获的所有接口

    request: 接口中包含请求信息

    response:接口中的响应信息

    因此我们可以使用代码解析出其中的请求即请求数据、请求头等进行“重放”请求。

    最后:你如果是对python自动化测试、web自动化、接口自动化、移动端自动化、面试经验交流等等感兴趣的测试人,可以 点这自行获取…

    相关文章

      网友评论

        本文标题:抓包导出的har格式解析

        本文链接:https://www.haomeiwen.com/subject/xwowurtx.html