【问题标题】:Parse json fields to variables将 json 字段解析为变量
【发布时间】: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


【解决方案1】:

首先你需要解析响应的 JSON。

data = alertDetails.json() # use convenience method

然后像嵌套字典一样访问你想要的字段。

url = data['Details']['Source'].get('URL')
print (f'Phishing URL: {url}')
found_date = data.get('FoundDate')
print(f'Found date: {found_date}')

这假设(基于您显示的错误)DeatailsSource 将始终存在,只有 URL 键可能丢失。为了安全起见,您可以这样做

url = data.get('Details', {}).get('Source', {}).get('URL')
print (f'Phishing URL: {url}')

或使用 try/except:

try:
    url = data['Details']['Source']['URL']
except KeyError:
    url = None
print (f'Phishing URL: {url}')

【讨论】:

  • 结果:Traceback (most recent call last): File "cefexample.py", line 74, in &lt;module&gt; url = data['Details']['Source']['URL'] KeyError: 'URL' 奇怪的是,使用其他值它可以工作:如果我使用:url = data['Details']['Source']['NetworkType'] 它可以工作:python cefexample.py 结果:ClearWeb 2021-02-18T16:02:20.926Z
  • 它适用于提供的示例数据。特定响应是否可能缺少 URL 键?您可以使用dict.get() 方法来处理丢失的键
  • 我做到了:data = alertDetails.json() data2=data.get('Details') data3=data2.get('Source') data4= data3.get('URL') print ('Phishing URL: '+str(data4)) 并且它有效! :D 但是,如您所见,它是如此之长。有一种方法可以做同样的事情,但要总结一下?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2020-06-16
相关资源
最近更新 更多