Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中显示JSON响应中的特定字段(通过解析)_Python_Json_Urllib2 - Fatal编程技术网

在Python中显示JSON响应中的特定字段(通过解析)

在Python中显示JSON响应中的特定字段(通过解析),python,json,urllib2,Python,Json,Urllib2,我希望在cli的打印列表中显示从以下URL返回的JSON响应中的特定字段: 我能够使用请求库输出JSON响应,如下所示: import urllib, json url = "http://www.cvedetails.com/json-feed.php?numrows=5&vendor_id=26&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfilein

我希望在cli的打印列表中显示从以下URL返回的JSON响应中的特定字段:

我能够使用请求库输出JSON响应,如下所示:

import urllib, json
url = "http://www.cvedetails.com/json-feed.php?numrows=5&vendor_id=26&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=0&opsqli=1&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=2&cvssscoremin=0"
response = urllib.urlopen();
data = json.loads(response.read())
print data
但是,我不确定将变量放在何处,以告诉“打印数据”命令要显示哪些特定字段

JSON响应就是这样形成的,每个值前面都有一个“u”标记:

{u'update_date': u'2014-11-18', u'cve_id': u'CVE-2014-4114', u'exploit_count': u'3', u'summary': u'Microsoft Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1 allow remote attackers to execute arbitrary code via a crafted OLE object in an Office document, as exploited in the wild with a "Sandworm" attack in June through October 2014, aka "Windows OLE Remote Code Execution Vulnerability."', u'url': u'http://www.cvedetails.com/cve/CVE-2014-4114/', u'publish_date': u'2014-10-15', u'cvss_score': u'9.3', u'cwe_id': u'20'}`
我希望有这样一个列表视图:

update_date:
cve_id:
exploit_count:

等等。

语法意味着你有Unicode字符串。它们非常像常规的
“…”
字符串;当内容只是ASCII文本时,没有区别。这里有一个字典列表,所以索引到每个字典,然后只使用字符串作为键:

print data[0]['update_date']
要打印该特定值,或在所有字典项上循环,请执行以下操作:

for index, entry in enumerate(data, 1):
    print 'Item {}'.format(index)
    for name, value in entry.items():
        print '    {}: {}'.format(name, value)
其中,我使用
str.format()
模板打印每个项目,在键名后加一个冒号,并使用
enumerate()
为每个项目指定一个数字

使用您提供的URL中的数据进行演示:

>>> for index, entry in enumerate(data, 1):
...     print 'Item {}'.format(index)
...     for name, value in entry.items():
...         print '    {}: {}'.format(name, value)
... 
Item 1
    update_date: 2014-11-18
    cve_id: CVE-2014-4114
    exploit_count: 3
    summary: Microsoft Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1 allow remote attackers to execute arbitrary code via a crafted OLE object in an Office document, as exploited in the wild with a "Sandworm" attack in June through October 2014, aka "Windows OLE Remote Code Execution Vulnerability."
    url: http://www.cvedetails.com/cve/CVE-2014-4114/
    publish_date: 2014-10-15
    cvss_score: 9.3
    cwe_id: 20
Item 2
    update_date: 2014-05-16
    cve_id: CVE-2014-0322
    exploit_count: 2
    summary: Use-after-free vulnerability in Microsoft Internet Explorer 9 and 10 allows remote attackers to execute arbitrary code via vectors involving crafted JavaScript code, CMarkup, and the onpropertychange attribute of a script element, as exploited in the wild in January and February 2014.
    url: http://www.cvedetails.com/cve/CVE-2014-0322/
    publish_date: 2014-02-14
    cvss_score: 9.3
    cwe_id: 399
Item 3
    update_date: 2014-04-14
    cve_id: CVE-2014-2671
    exploit_count: 1
    summary: Microsoft Windows Media Player (WMP) 11.0.5721.5230 allows remote attackers to cause a denial of service (memory corruption) or possibly have unspecified other impact via a crafted WAV file.
    url: http://www.cvedetails.com/cve/CVE-2014-2671/
    publish_date: 2014-03-31
    cvss_score: 6.8
    cwe_id: 119
Item 4
    update_date: 2014-03-26
    cve_id: CVE-2014-0307
    exploit_count: 1
    summary: Use-after-free vulnerability in Microsoft Internet Explorer 9 allows remote attackers to execute arbitrary code or cause a denial of service (memory corruption) via a certain sequence of manipulations of a TextRange element, aka "Internet Explorer Memory Corruption Vulnerability."
    url: http://www.cvedetails.com/cve/CVE-2014-0307/
    publish_date: 2014-03-12
    cvss_score: 9.3
    cwe_id: 119
Item 5
    update_date: 2014-01-17
    cve_id: CVE-2013-3906
    exploit_count: 1
    summary: GDI+ in Microsoft Windows Vista SP2 and Server 2008 SP2; Office 2003 SP3, 2007 SP3, and 2010 SP1 and SP2; Office Compatibility Pack SP3; and Lync 2010, 2010 Attendee, 2013, and Basic 2013 allows remote attackers to execute arbitrary code via a crafted TIFF image, as demonstrated by an image in a Word document, and exploited in the wild in October and November 2013.
    url: http://www.cvedetails.com/cve/CVE-2013-3906/
    publish_date: 2013-11-06
    cvss_score: 9.3
    cwe_id: 94
请注意,发布的代码不会运行,因为您实际上没有将URL传递给
urllib.urlopen()
函数:

url = "http://www.cvedetails.com/json-feed.php?numrows=5&vendor_id=26&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=0&opsqli=1&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=2&cvssscoremin=0"
response = urllib.urlopen();
分号也不需要;改为传入
url
参数:

response = urllib.urlopen(url)

实际上,该URL返回一个包含字典的列表。您缺少真实响应中的
[…]
括号。谢谢Martijn。当我尝试打印由两部分组成的输出时,它抛出以下错误:
response=urllib.urlopen();TypeError:urlopen()至少接受1个参数(给定0)
导入urllib,json url=“JSONREQUESTURL”response=urllib.urlopen();data=json.load(response.read())表示数据中的名称、值。items():打印“{}:{}”。格式(名称、值)
我需要URLLIB2才能工作吗?@cam_I_鲁尼:您没有将URL传递到
URL.urlopen()
。。。你有没有试着使用代码?是的,我正在传递完整的URL,但是由于字符限制,没有包含在注释代码中。是的,我做了上面的操作,它可以工作,但只打印返回的5的最后一个JSON响应。我假设这是为了计算打印的数量,我会尝试解决这个问题。谢谢你的帮助,玛蒂恩!