[英]How do I query JSON data from an API with multiple filters?
我正在從JSON API檢索數據。
數據由應用程序列表組成,每個應用程序都具有許多屬性。
我只想要與query_input
匹配且不受信任的應用程序(請參閱API文檔圖片): 我想要兩者的交集。 我只想要同時符合這兩個條件的應用程序。 如果我使用:
parameters = {"query" : query_input, "trusted" : "false", "limit" : 3}
request = requests.get("http://(certain api link)", params = parameters)
print(json.dumps(request.json(), indent = 4))
如果我使用上面的代碼,並將query_input
設置為“ facebook”, query_input
得到以下信息:
{
"info": {
"status": "OK",
"time": {
"seconds": 0.068413019180298,
"human": "68 milliseconds"
}
},
"datalist": {
"total": 1000,
"count": 3,
"offset": 0,
"limit": 3,
"next": 3,
"hidden": 0,
"loaded": true,
"list": [
{
"id": 44893072,
"name": "Facebook",
"package": "com.facebook.katana",
"uname": "facebook",
"size": 56986654,
"icon": "http://(certain link)",
"graphic": "http://(certain link)",
"added": "2013-02-15 03:30:51",
"modified": "2019-03-21 02:24:03",
"updated": "2019-03-21 23:01:10",
"uptype": "xxxxxxxxx",
"store": {
"id": 65518,
"name": "mark8",
"avatar": "http://(certain link)",
"appearance": {
"theme": "lightsky",
"description": ""
},
"stats": {
"apps": 3552,
"subscribers": 1110865,
"downloads": 505861392
}
},
"file": {
"vername": "215.0.0.0.37",
"vercode": 146691804,
"md5sum": "bc0ed01e35b7f888bdaa34eb2c2a2e1f",
"filesize": 56986654,
"path": "http://(certain link)",
"path_alt": "(certain link)",
"malware": {
"rank": "TRUSTED"
}
},
"stats": {
"downloads": 10272286,
"pdownloads": 122342832,
"rating": {
"avg": 4.2,
"total": 4966
},
"prating": {
"avg": 4.17,
"total": 20811
}
},
"has_versions": true,
"obb": null,
"appcoins": {
"advertising": false,
"billing": false
}
},
{
"id": 44918187,
"name": "Facebook Lite",
"package": "com.facebook.lite",
"uname": "facebook-lite",
"size": 1292422,
"icon": "http://(certain link)",
"graphic": "(certain link)",
"added": "2017-09-21 11:39:42",
"modified": "2019-03-21 19:06:13",
"updated": "2019-03-21 23:04:44",
"uptype": "xxxxxxx",
"store": {
"id": 1618942,
"name": "hazel777",
"avatar": "http://(certain link)",
"appearance": {
"theme": "pink",
"description": "Thanks 4 being a follower"
},
"stats": {
"apps": 4098,
"subscribers": 63185,
"downloads": 28170613
}
},
"file": {
"vername": "139.0.0.9.85",
"vercode": 146791354,
"md5sum": "xxxxxxxxxxxxxxxxxxxxxxxx",
"filesize": 1292422,
"path": "http://(certain link)",
"path_alt": "http://(certain link)",
"malware": {
"rank": "TRUSTED"
}
},
"stats": {
"downloads": 18393,
"pdownloads": 23956738,
"rating": {
"avg": 3,
"total": 2
},
"prating": {
"avg": 4.28,
"total": 2225
}
},
"has_versions": true,
"obb": null,
"appcoins": {
"advertising": false,
"billing": false
}
},
{
"id": 3126316,
"name": "Facebook",
"package": "com.sonyericsson.androidapp.fbsetupwizard",
"uname": "xperia-fb-setup",
"size": 181391,
"icon": "http://(certain link)",
"graphic": null,
"added": "2013-03-26 03:26:09",
"modified": "2013-05-05 17:59:53",
"updated": "2019-03-06 19:05:21",
"uptype": "dropbox",
"store": {
"id": 8655,
"name": "vitonline",
"avatar": "http://(certain link)",
"appearance": {
"theme": "light-blue",
"description": ""
},
"stats": {
"apps": 8609,
"subscribers": 27653,
"downloads": 3807977
}
},
"file": {
"vername": "5.0.A.0.10",
"vercode": 10485770,
"md5sum": "xxxxxxxxxxxxxxxx",
"filesize": 181391,
"path": "http://(certain link)",
"path_alt": "http://(certain link)",
"malware": {
"rank": "TRUSTED"
}
},
"stats": {
"downloads": 218559,
"pdownloads": 269179,
"rating": {
"avg": 3.75,
"total": 44
},
"prating": {
"avg": 3.63,
"total": 98
}
},
"has_versions": true,
"obb": null,
"appcoins": {
"advertising": false,
"billing": false
}
}
]
}
}
有點工作。
我得到了與Facebook相關的應用程序,並且因為"limit" : 3
,所以它僅顯示3個應用程序(所有應用程序都在“ datalist”>“ list”中,每個應用程序均以“ id”開頭並以“ billing”結束)。
我應該只獲得惡意軟件排名為“未知”的應用程序,但是我不想要惡意軟件排名為“ TRUSTED”的應用程序。
API文檔將false
表示為trusted
的JSON布爾值,因此在Python中,您應該使用布爾值False
而不是字符串值"false"
,服務器將其視為真值而不是假值:
parameters = {"query" : query_input, "trusted" : False, "limit" : 3}
我認為文檔有些誤導。 如果將您的可信參數設置為true,它將List trusted apps only
。 如果將其設置為false,則所有應用程序均為Trusted and Unknown。
無法選擇僅從來源獲取未知應用程序。
我已經嘗試過查詢Facebook,並且所有應用都返回“受信任”狀態。 可能是因為該來源中沒有不可信/未知的應用。 使用terrarium
作為查詢,您可以使用trusted=false
獲得一些未知的信任級別,然后在trusted=true
時將其刪除
因此,您似乎需要手動獲取所有結果,然后在惡意軟件排名不等於“可信”的情況下過濾字典
app_data = request.json()
untrusted = [app_entry for app_entry in app_data['datalist']['list'] if app_entry['file']['malware']['rank'].upper() != 'TRUSTED']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.