【问题标题】:JSON - parsing independently named sub-objectsJSON - 解析独立命名的子对象
【发布时间】:2017-08-28 14:25:01
【问题描述】:

我正在使用 VirusTotal API 尝试编写一个脚本来扫描和报告结果。我遇到的问题是代码的 SCANS 部分,我可以提取有多少是正面的,但我想输出每个正面的扫描和结果(病毒名称、网络钓鱼等)。

示例 JSON:

{
'response_code': 1,
'verbose_msg': 'Scan finished, scan information embedded in this object',
'scan_id':'1db0ad7dbcec0676710ea0eaacd35d5e471d3e11944d53bcbd31f0cbd11bce31-1390467782',
'permalink': 'https://www.virustotal.com/url/__urlsha256__/analysis/1390467782/',
'url': 'http://www.virustotal.com/',
'scan_date': '2014-01-23 09:03:02',
'filescan_id': null,
'positives': 0,
'total': 51,
'scans': {
      'CLEAN MX': {
      'detected': false,
      'result': 'clean site'
       },
      'MalwarePatrol': {
      'detected': false,
      'result': 'clean site'
      }
 }
 }

我编写的代码似乎没有将所有扫描信息传递到循环中,我似乎唯一可以访问的是未检测到每个扫描仪名称/结果信息的 AV 扫描仪名称。第一次使用 JSON api,感谢任何帮助。

params = {'apikey': apikey, 'resource':line}
response = requests.get(vt_report_url, params=params)
result = response.json()

if result['positives'] != "0":
    print "malware detection"
    for avList in result['scans']:
        if avList[1] == 'true':
            print str(avList[0]) + " - "+ str(avList[2])

【问题讨论】:

    标签: python arrays json


    【解决方案1】:

    从代码中我可以看到scans 是一个字典,因此您需要使用dict.items() 循环遍历它。

    代码:

    params = {'apikey': apikey, 'resource':line}
    response = requests.get(vt_report_url, params=params)
    result = response.json()
    
    if result['positives'] != 0:
        print "malware detection" 
        for key, value in result['scans'].items():
            if value['detected'] == False:
                print str(key) + " - "+ str(value['result'])
    

    输出:

    malware detection
    CLEAN MX {'detected': False, 'result': 'clean site'}
    

    【讨论】:

    • 我试了一下这段代码,它最初看起来可以工作,但它并没有像我期望的那样将项目传递到一个数组中。我在 if 语句上遇到了一个键错误,所以我在检查之前打印出该值,它没有按预期将项目拆分,它返回一行 {u'detected': False, u'result': u'clean site'}
    • @nilats 你从来没有提到你需要什么输出。
    • 很公平,我的目标是以“CLEAN MX - clean site”/n“MalwarePatrol - clean site”的格式提取各种扫描的 AV 名称和结果
    • @nilats 检查我的代码,有一件事,我改了'positives': 2 否则代码不会进入第一个if条件。
    • 此代码只是使用来自 VT api 站点的示例 JSON 的示例,我正在使用的代码是扫描一个不想包含它的已知恶意链接。因此,积极因素总体上不是问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2012-01-29
    • 2017-08-11
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多