我很难使用json库从长文本中检索json数据。数据通过curl (text.txt)从cisco搜索工具api中检索。
我的代码只识别根元素。子元素不被检索。
我不知道我错过了什么。
代码:
with open('text.txt', 'r', encoding='utf8', errors='ignore') as file:
filecontent = file.readlines()
for line in filecontent:
if re.search("^\{\"advisories\"(.*?)\}", line):
y = json.loads(line)
for key, value in y.items():
print("Key:" + key)输出
root@server#python3 security.py
Key:advisories
root@server#text.txt
{"advisories":[{"advisoryId":"cisco-sa-fxo-pattern-bypass-jUXgygYv","advisoryTitle":"Cisco IOS and IOS XE Software FXO Interface Destination Pattern Bypass Vulnerability","bugIDs":["CSCvw53542"],"ipsSignatures":["NA"],"cves":["CVE-2021-34705"],"cvrfUrl":"https://tools.cisco.com/security/center/contentxml/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv/cvrf/cisco-sa-fxo-pattern-bypass-jUXgygYv_cvrf.xml","cvssBaseScore":"5.3","cwe":["CWE-232"],"iosRelease":["15.1(4)M4"],"firstFixed":["15.7(3)M8"],"firstPublished":"2021-09-22T16:00:00","lastUpdated":"2021-09-22T16:00:00","status":"Final","version":"1.0","productNames":["Cisco IOS ","Cisco IOS XE Software ","Cisco IOS 12.3(9a)","Cisco IOS 12.3(15)",(...),"Cisco IOS XE Software 17.3.1w","Cisco IOS XE Software 17.3.2a","Cisco IOS XE Software 17.3.1x","Cisco IOS XE Software 17.3.1z","Cisco IOS XE Software 17.4.1","Cisco IOS XE Software 17.4.1a","Cisco IOS XE Software 17.4.1b","Cisco IOS XE Software 17.4.1c","Cisco IOS XE Software 17.6.1w"],"publicationUrl":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv","sir":"High","summary":"\n<p>A vulnerability in the Voice Telephony Service Provider (VTSP) service of Cisco IOS Software and Cisco IOS XE Software could allow an unauthenticated, remote attacker to bypass configured destination patterns and dial arbitrary numbers.</p>\n<p>This vulnerability is due to insufficient validation of dial strings at Foreign Exchange Office (FXO) interfaces. An attacker could exploit this vulnerability by sending a malformed dial string to an affected device via either the ISDN protocol or SIP. A successful exploit could allow the attacker to conduct toll fraud, resulting in unexpected financial impact to affected customers.</p>\n<p>Cisco has released software updates that address this vulnerability. There are no workarounds that address this vulnerability.</p>\n<p>This advisory is available at the following link:<br><a href=\"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv\" target=\"_blank\">https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv</a></p>\n\n<p>This advisory is part of the September 2021 release of the Cisco IOS and IOS XE Software Security Advisory Bundled Publication. For a complete list of the advisories and links to them, see <a href=\"https://tools.cisco.com/security/center/viewErp.x?alertId=ERP-74581\">Cisco Event Response: September 2021 Semiannual Cisco IOS and IOS XE Software Security Advisory Bundled Publication.</a></p>\n\n"},{"advisoryId":"cisco-sa-info-disclosure-V4BmJBNF",etc (it repeats)任何帮助都是非常感谢的。谢谢
发布于 2022-02-18 23:55:14
您似乎希望在Python中使用这些建议,或者重新格式化并打印出来。
需要理解的最重要的事情是,json.load将为您完成这里的所有工作,因此您不必使用re或readlines。
下面是一个例子:
with open('test.txt', errors='ignore') as file:
data = json.load(file)
for advisory in data['advisories']:
print('advisoryId = ' + repr(advisory['advisoryId']))发布于 2022-02-19 00:51:46
首先,您需要验证json语法。
http://json.parser.online.fr/
然后
with open('text.txt','r', encoding='utf8', errors='ignore') as file:
data = json.load(file)
for advisories in data['advisories']:
keyList = advisories.keys()
for key in keyList:
print(key,'::', advisories[key])结果是
advisoryId :思科-sa-fxo-模式-旁路-jUXgygYv
advisoryTitle :思科IOS和IOS XE软件FXO接口目的地
bugIDs ::CSCvw53542
ipsSignatures ::'NA‘
.
https://stackoverflow.com/questions/71181028
复制相似问题