我得到了KeyError。

huangapple go评论63阅读模式
英文:

I am getting KeyError

问题

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

英文:

> Traceback (most recent call last): File
> "C:\Users\user\Desktop\depremleri-goruntuleme-main\main.py", line 15,
> in <module>
> 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+&quot;            5 1 2 4 - O f f i c a l \n&quot;)
    print(Fore.RED+&quot;T&#252;r         &quot;+Fore.CYAN+&quot;Tarih &amp; Saat           &quot;+Fore.GREEN+&quot;Yer           &quot;+Fore.RED+&quot;B&#252;y&#252;kl&#252;k        &quot;+Fore.YELLOW+&quot;Derinlik&quot;)
    print(&quot;-------------------------------------------------------------------------------&quot;)

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

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

    sleep(13)
    system(&quot;cls&quot;)
    sleep(0.6)

答案1

得分: 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 - Official \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)
英文:

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+&quot;            5 1 2 4 - O f f i c a l \n&quot;)
    print(Fore.RED+&quot;T&#252;r         &quot;+Fore.CYAN+&quot;Tarih &amp; Saat           &quot;+Fore.GREEN+&quot;Yer           &quot;+Fore.RED+&quot;B&#252;y&#252;kl&#252;k        &quot;+Fore.YELLOW+&quot;Derinlik&quot;)
    print(&quot;-------------------------------------------------------------------------------&quot;)

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

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

    sleep(13)
    system(&quot;cls&quot;)
    sleep(0.6)

huangapple
  • 本文由 发表于 2023年3月4日 01:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定