【发布时间】:2021-11-08 06:26:47
【问题描述】:
我正在尝试从 API 查询解析 json 输出的字段。
示例 json:
{
"_id":"611a7b651571d300074b9875",
"Details":{
"Source":{
"Type":"WHOIS servers",
"URL":"http://tiktea.com.au",
"NetworkType":"ClearWeb"
},
"Type":"Phishing",
"SubType":"RegisteredSuspiciousDomain",
"Severity":"Medium"
},
"Assignees":[
],
"FoundDate":"2021-08-16T14:51:17.747Z",
"Assets":[
{
"Type":"Domains",
"Value":"iktea.net"
}
],
"TakedownStatus":"NotSent",
"IsFlagged":false,
"Closed":{
"IsClosed":false
}
}
我想解析一些字段,得到类似的东西:
URL: http://tiktea.com.au
FoundDate: 2021-08-16T14:51:17.747Z"
所以我尝试了这个,但没有成功:Selecting fields from JSON output
这是我的python代码:
import requests
import json
#UserID + Api Key
auth = ('USERID','APIKEY')
headers = { 'Content-Type': "application/json", }
url = "https://api.intsights.com/public/v1/data/alerts/alerts-list?alertType=Phishing
alerts = requests.request("GET", url, auth = auth, headers=headers)
for alertID in alerts.text.split('","'):
url = "https://api.intsights.com/public/v1/data/alerts/get-alert/"+alertID #<-----This is the request that extract the JSON
alertDetails = requests.request("GET", url, auth = auth, headers=headers)
dict = alertDetails.text
url=dict['Assets'][0]['Value'] #<---------Trying to parse the desired value
print(url)
这是结果:
Traceback (most recent call last):
File "cefexample.py", line 73, in <module>
url=dict['Assets'][0]['Value']
TypeError: string indices must be integers
有什么建议吗?
【问题讨论】:
-
您能否更新您的问题以显示
alerts响应请求的样子? -
@ davidculbreth'警报'的响应是:[6132fab73125c00078e7180],“6133790744464078f665a”,“6133791144b6650007c6a3f4”,61337911c406420008499cb9“,61337c8792587f000b68c127”] span>
标签: python arrays json parsing