我制作了一個小腳本來獲取加入我的Minecraft服務器的人的UUID,然后通過PlayerDB API通過發布請求運行它們:https://playerdb.co/api/player/minecraft/ * 其中 * 是播放器 UUID,但它回傳隨機 404 錯誤。
錯誤在 player_name_history 行上運行,并在“玩家”處突出顯示 KeyError:
def getPlayerData(UUID_list):
baseurl = "https://playerdb.co/api/player/minecraft/"
player_names=[]
icon_list = []
for UUID in UUID_list:
response = requests.post(baseurl UUID)
print("url posted:",baseurl UUID)
print(response.status_code)
responseObject = response.json()
with open('responseObject.json','w') as f:
json.dump(responseObject, f)
player_name_history = responseObject['data']['player']['meta']['name_history']
for x in player_name_history:
player_name = x['name'] #iterates through all names, last name will not be overrwritten
player_names.append(player_name)
icon_link = responseObject['data']['player']['avatar']
icon_list.append(icon_link)
return player_names, icon_list
for x in player_name_history:
player_name = x['name'] #iterates through all names, last name will not be overrwritten
player_names.append(player_name)
icon_link = responseObject['data']['player']['avatar']
icon_list.append(icon_link)
return player_names, icon_list
您可以將我的 UUID 作為串列傳遞給函式進行測驗:
['bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310']
它的作業示例:
url posted: https://playerdb.co/api/player/minecraft/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310
200
(['zonkedzolt'], ['https://crafthead.net/avatar/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310'])
它不作業的例子:
url posted: https://playerdb.co/api/player/minecraft/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310
404
Traceback (most recent call last):
File "g:\*\getPlayerData.py", line 74, in <module>
print(getPlayerData(UUID_list))
File "g:\*\getPlayerData.py", line 58, in getPlayerData
player_name_history = responseObject['data']['player']['meta']['name_history']
KeyError: 'player'
json轉儲:{"message": "", "code": "api.404", "data": {}, "success": false, "error": false}
我懷疑它可能是我的代碼的原因是因為當我收到錯誤時,如果我 ctrl 左鍵單擊“url 已發布:”行上的鏈接,它會顯示正確的結果。
uj5u.com熱心網友回復:
如果您從 API 收到如下錯誤訊息:
{"message": "", "code": "api.404", "data": {}, "success": false, "error": false}
嘗試分別請求每個 UUID,一旦獲得良好回應,請嘗試使用您的程式運行它。API似乎快取了第一次詢問的UUID,但是如果您再次詢問太快會回傳上述錯誤。
偶爾我仍然會遇到上面的錯誤,但重新請求會解決它。您可以創建一個 while 回圈以繼續請求,直到收到良好的回應。這是我制作的while回圈:
goodResponse = False
while goodResponse == False:
response = requests.post(baseurl UUID)
print("url posted:",baseurl UUID)
print(response.status_code)
responseObject = response.json()
if "player" in responseObject['data']: #checks if UUID has recieved a good response, otherwise loops until it does.
goodResponse = True #continue code here
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504341.html
