Search code examples
pythonkeyerror

I am getting KeyError


Traceback (most recent call last): File "C:\Users\user\Desktop\depremleri-goruntuleme-main\main.py", line 15, in for item in json["result"]: KeyError: 'result'

im getting this keyerror error and I don't know how to solve it

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)

Solution

  • here is your complete code, I just replaced post with get method.

    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)