我收到 KeyError

问题描述 投票:0回答:2

Traceback(最后一次调用):文件 “C:\Users\user\Desktop\depremleri-goruntuleme-main\main.py”,第 15 行, 在 对于 json["result"] 中的项目:KeyError: 'result'

我收到这个 keyerror 错误,我不知道如何解决它谁能紧急帮助我( 下面是我的代码)

import json
from os import system
from time import sleep
import requests                                 
from colorama import init, Fore
init(autoreset=True)

while True:

    print(Fore.RED+"            5 1 2 4 - O f f i c a l \n")
    print(Fore.RED+"Tür         "+Fore.CYAN+"Tarih & Saat           "+Fore.GREEN+"Yer           "+Fore.RED+"Büyüklük        "+Fore.YELLOW+"Derinlik")
    print("-------------------------------------------------------------------------------")

    json = requests.post("https://api.orhanaydogdu.com.tr/deprem/live.php").json()
    for item in json["result"]:
        tarih = item["date"]
        yer = item["lokasyon"]
        büyüklük = item["mag"]
        derinlik = item["depth"]

        print(Fore.RED+"Deprem ! "+Fore.CYAN+tarih+Fore.GREEN+" "+yer+Fore.RED+" "+str(büyüklük)+Fore.YELLOW+" "+str(derinlik)) #Depremleri yazdırıyoruz

    sleep(13)
    system("cls")
    sleep(0.6)
python keyerror
2个回答
0
投票

这是你的完整代码,我只是用 get 方法替换了 post。

import json
from os import system
from time import sleep
import requests                                 
from colorama import init, Fore
init(autoreset=True)

while True:

    print(Fore.RED+"            5 1 2 4 - O f f i c a l \n")
    print(Fore.RED+"Tür         "+Fore.CYAN+"Tarih & Saat           "+Fore.GREEN+"Yer           "+Fore.RED+"Büyüklük        "+Fore.YELLOW+"Derinlik")
    print("-------------------------------------------------------------------------------")

    json_data = requests.get("https://api.orhanaydogdu.com.tr/deprem/live.php").json()
    for item in json_data ["result"]:
        tarih = item["date"]
        yer = item["lokasyon"]
        büyüklük = item["mag"]
        derinlik = item["depth"]

        print(Fore.RED+"Deprem ! "+Fore.CYAN+tarih+Fore.GREEN+" "+yer+Fore.RED+" "+str(büyüklük)+Fore.YELLOW+" "+str(derinlik)) #Depremleri yazdırıyoruz

    sleep(13)
    system("cls")
    sleep(0.6)

0
投票

使用get方法代替post,解决方法如下:

json_data = requests.get("https://api.orhanaydogdu.com.tr/deprem/live.php").json()
print(json_data['result'])
© www.soinside.com 2019 - 2024. All rights reserved.