简体   繁体   中英

How to read/fix incorrect har file form to be read by python?

With python I want to read the content of a har file, but the har file seems to be in some incorrect format. The har file I have looks like this:

"{\n  \"log\": {\n    \"version\": \"1.2\",\n    \"creator\": {\n      \"name\": \"Selenium Wire HAR dump\",\n      \"version\": \"0.1\",\n      \"comment\": \"Selenium Wire version 4.6.3\"\n    },\n    \"entries\": [\n      {\n        \"startedDateTime\": \"2022-03-21T15:44:16.496471+00:00\",\n        \"time\": 200,\n        \"request\": {\n          \"method\": \"GET\",\n          \"url\": \"http://sscx-portal.ocp.bbp.epfl.ch/sscx-portal/digital-reconstructions/neurons?brain_region=S1DZO&layer=L23&etype=bAC&mtype=L23_LBC&memodel=L23_LBC_bAC_2\",\n          \"httpVersion\": \"HTTP/1.1\",\n          \"cookies\": [],\n          \"headers\": [\n            {\n              \"name\": \"Host\",\n              \"value\": \"sscx-portal.ocp.bbp.epfl.ch\"\n            },\n            {\n              \"name\": \"Upgrade-Insecure-Requests\",\n              \"value\": \"1\"\n            },\n            {\n              \"name\": \"User-Agent\",\n              \"value\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36\"\n            },\n            {\n              \"name\": \"Accept\",\n              \"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\"\n            },\n            {\n              \"name\": \"Accept-Encoding\",\n              \"value\": \"gzip, deflate\"\n            },\n            {\n              \"name\": \"Accept-Language\",\n              \"value\": \"en-US,en;q=0.9\"\n            }\n          ],\n          \"queryString\": [\n            {\n              \"name\": \"brain_region\",\n              \"value\": \"S1DZO\"\n            },\n            {\n              \"name\": \"layer\",\n              \"value\": \"L23\"\n            },\n            {\n              \"name\": \"etype\",\n              \"value\": \"bAC\"\n            },\n            {\n              \"name\": \"mtype\",\n              \"value\": \"L23_LBC\"\n            },\n            {\n              \"name\": \"memodel\",\n              \"value\":...

and therefore I cannot read it as json.

How to convert that into json?

The solution is to "un-escape" that bunch of text using ast . And then it is possible to read it as json:

with open("myfile.har") as f:
    data = json.loads(ast.literal_eval(f.read()))

print(data["log"].keys())

which give the output

dict_keys(['version', 'creator', 'entries'])

and then you can take it from there.

I advise against usingharalyzer , as it does not seem maintained (still python2 syntax), and it does not work as the example shows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM