I am getting KeyError

Question:

Traceback (most recent call last): File
"C:UsersuserDesktopdepremleri-goruntuleme-mainmain.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)
Asked By: Snowflake

||

Answers:

Use get method instead of post, here is the solution:

json_data = requests.get("https://api.orhanaydogdu.com.tr/deprem/live.php").json()
print(json_data['result'])
Answered By: Ajeet Verma

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)
Answered By: Ajeet Verma
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.