Created
July 28, 2020 14:49
-
-
Save maesoser/26e8beec5c14acbd278a2af7e6b64520 to your computer and use it in GitHub Desktop.
Extract resources from har files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import jsbeautifier | |
import argparse | |
def get_args(): | |
parser = argparse.ArgumentParser(description="Extract resources from HAR file") | |
parser.add_argument('--har', required=True, help='Har File') | |
parser.add_argument('--filter', required=True, help='What the file needs to include to be downloaded', default=".js") | |
args = parser.parse_args() | |
return args | |
args = get_args() | |
# jq '.log | .entries | .[] | .request | select(.method | contains("POST")) | .postData' example.har | |
print("Opening {}".format(args.har)) | |
with open(args.har) as json_file: | |
data = json.load(json_file) | |
for p in data["log"]['entries']: | |
url = p['request']["url"] | |
method = p['request']["method"] | |
filename = url.split("/")[-1] | |
print("{}: {}".format(method, url)) | |
if method == "GET" and filename.find(args.filter)!=-1: | |
print("\tExtracting to {}".format(filename)) | |
content = p['response']["content"]["text"] | |
if filename.find(".js") | |
content = jsbeautifier.beautify(content) | |
text_file = open(filename, "w") | |
text_file.write(content) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment