Skip to content

Instantly share code, notes, and snippets.

@zhiqiang21
Created December 28, 2016 03:42
Show Gist options
  • Save zhiqiang21/462c793bac3585dbcdd2cea53af49b05 to your computer and use it in GitHub Desktop.
Save zhiqiang21/462c793bac3585dbcdd2cea53af49b05 to your computer and use it in GitHub Desktop.
解析浏览器导出的har文件
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
def readHarFile():
hostList = []
outputPath = ''
filePath = raw_input('输入文件路径+名字:')
outputType = raw_input('1.直接输出为txt文件;2.直接输出在控制台;(请输入 1 or 2 )')
if str(outputType) is '1':
outputPath = raw_input('输出文件路径+名字:')
with open(filePath, 'r') as readObj:
harDirct = json.loads(readObj.read())
requestList = harDirct['log']['entries']
for item in requestList:
urlString = (item['request']['url'])
start = urlString.index('://')
tempStr = urlString[start + 3:]
end = tempStr.index('/')
resultStr = tempStr[:end]
# 判断是否是www开头的域名
if 'www' in resultStr:
resultStr = resultStr[4:]
if resultStr not in hostList:
hostList.append(resultStr)
if str(outputType) is '1':
with open(outputPath, 'w') as ff:
for item in hostList:
ff.write('"' + item + '",' + '\n')
else:
print '//=============host start=============='
for item in hostList:
print '"' + item + '",'
print '//=============host end================'
if __name__ == '__main__':
readHarFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment