我試圖決議一個API查詢輸出的json欄位。
示例json:
{
"_id":"611a7b651571d300074b9875",
"詳細資訊":{
"來源":{
"Type":"WHOIS servers"。
"URL"/span>:"http://tiktea.com.au"/span>。
"NetworkType":"ClearWeb"。
},
"Type"/span>:"Phishing"。
"SubType":"RegisteredSuspiciousDomain"。
"嚴重程度":"中等"。
},
"受讓人":[
],
"FoundDate":"2021-08-16T14:51:17.747Z",
"Assets":[
{
"Type":"Domains",
"Value":"iktea.net".
}
],
"TakedownStatus"/span>:"NotSent"。
"IsFlagged":false。
"Closed":{
"IsClosed":false
}
}
我想決議一些欄位,得到的結果是:
我想決議一些欄位。
URL: http://tiktea.com.au
FoundDate: 2021-08-16T14:51:17.747Z"
所以我嘗試了這個,但沒有成功。從JSON輸出中選擇欄位
這是我的python代碼:
這是我的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 #<-----這就是提取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):
檔案 "cefexample.py", line 73, in < module>
url=dict['Assets'][0] ['Value']
TypeError: 字串索引必須是整數
任何建議?
uj5u.com熱心網友回復:
首先,你需要決議作為回應的JSON。
data = alertDetails.json() # use convenient method
然后像一個嵌套的dict一樣訪問你想要的欄位。
url = data['Details']['Source'].get('URL')
print (f'Phishing URL: {url}')
found_date = data.get('FoundDate')
print(f'Found date: {found_date}')
這假設(基于你顯示的錯誤),Deatails和Source將始終存在,只有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}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314848.html
標籤:
上一篇:在存盤程序內部傳遞輸入引數
下一篇:顫振無法加載影像資產
