如何将JSON的结果打印到输出文件中?

问题描述 投票:0回答:1

JSON文件的输出结果有多个响应,如下面所示。


"response_code":1

"scan_date":"2011-07-27 03:44:56"

"permalink":"https://www.virustotal.com/gui/file/1caea01fd9a6c6d12e5ca46007e25a4b1eff640060f45de8213e40aa5b47cd57/detection/f-1caea01fd9a6c6d12e5ca46007e25a4b1eff640060f45de8213e40aa5b47cd57-1311738296"

"verbose_msg":"Scan finished, information embedded"

"total":43

"positives":19

下面的代码会在JSON输出中获取 "positives "的值,然后打印到文件中。


    # DOES THE HASH EXISTS IN VT DATABASE?
    if response == 0:
        print(hash + ": UNKNOWN")
        file = open(output,"a")
        file.write(hash + " 0")
        file.write("\n")
        file.close()

    # DOES THE HASH EXISTS IN VT DATABASE?
    elif response == 1:
        positives = int(json_response.get("positives"))
        if positives >= 3:
            print(hash + ": MALICIOUS")
            file = open(output,"a")
            file.write(hash + " " + str(positives))
            file.write("\n")
            file.close()
        else:
            print(hash + ": NOT MALICIOUS")
            file = open(output,"a")
            file.write(hash + " 0")
            file.write("\n")
            file.close()
    else: print(hash + ": CAN NOT BE SEARCHED")

因此,当前代码的结果将是类似于下面的东西

0136b7453cedf600d6f6aab7900901d3            19

我想从JSON结果中获取 "permalink "的值,并打印在同一个输出文件中。因此,输出结果必须像下面这样


0136b7453cedf600d6f6aab7900901d3 19 https://www.virustotal.com/gui/file/1caea01fd9a6c6d12e5ca46007e25a4b1eff640060f45de8213e40aa5b47cd57/detection/f-1caea01fd9a6c6d12e5ca46007e25a4b1eff640060f45de8213e40aa5b47cd57-1311738296

我如何实现这个目标?

谅谅

python-3.x hash permalinks
1个回答
0
投票

你可以用读正值的方法来读它。

elif response == 1:
    positives = int(json_response.get("positives"))
    permalink = json_response.get("permalink")
    if positives >= 3:`enter code here`
        print(hash + ": MALICIOUS" + " | URL:" + permalink)
        file = open(output,"a")`enter code here`
        file.write(hash + " " + str(positives))
        file.write("\n")
        file.close()
© www.soinside.com 2019 - 2024. All rights reserved.