我得到了KeyError。

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

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

  1. import json
  2. from os import system
  3. from time import sleep
  4. import requests
  5. from colorama import init, Fore
  6. init(autoreset=True)
  7. while True:
  8. print(Fore.RED+&quot; 5 1 2 4 - O f f i c a l \n&quot;)
  9. 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;)
  10. print(&quot;-------------------------------------------------------------------------------&quot;)
  11. json = requests.post(&quot;https://api.orhanaydogdu.com.tr/deprem/live.php&quot;).json()
  12. for item in json[&quot;result&quot;]:
  13. tarih = item[&quot;date&quot;]
  14. yer = item[&quot;lokasyon&quot;]
  15. b&#252;y&#252;kl&#252;k = item[&quot;mag&quot;]
  16. derinlik = item[&quot;depth&quot;]
  17. 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
  18. sleep(13)
  19. system(&quot;cls&quot;)
  20. sleep(0.6)

答案1

得分: 0

以下是您的完整代码,我只用GET方法替换了POST。

  1. import json
  2. from os import system
  3. from time import sleep
  4. import requests
  5. from colorama import init, Fore
  6. init(autoreset=True)
  7. while True:
  8. print(Fore.RED + " 5 1 2 4 - Official \n")
  9. print(Fore.RED + "Tür " + Fore.CYAN + "Tarih & Saat " + Fore.GREEN + "Yer " + Fore.RED + "Büyüklük " + Fore.YELLOW + "Derinlik")
  10. print("-------------------------------------------------------------------------------")
  11. json_data = requests.get("https://api.orhanaydogdu.com.tr/deprem/live.php").json()
  12. for item in json_data["result"]:
  13. tarih = item["date"]
  14. yer = item["lokasyon"]
  15. büyüklük = item["mag"]
  16. derinlik = item["depth"]
  17. 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
  18. sleep(13)
  19. system("cls")
  20. sleep(0.6)
英文:

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

  1. import json
  2. from os import system
  3. from time import sleep
  4. import requests
  5. from colorama import init, Fore
  6. init(autoreset=True)
  7. while True:
  8. print(Fore.RED+&quot; 5 1 2 4 - O f f i c a l \n&quot;)
  9. 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;)
  10. print(&quot;-------------------------------------------------------------------------------&quot;)
  11. json_data = requests.get(&quot;https://api.orhanaydogdu.com.tr/deprem/live.php&quot;).json()
  12. for item in json_data [&quot;result&quot;]:
  13. tarih = item[&quot;date&quot;]
  14. yer = item[&quot;lokasyon&quot;]
  15. b&#252;y&#252;kl&#252;k = item[&quot;mag&quot;]
  16. derinlik = item[&quot;depth&quot;]
  17. 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
  18. sleep(13)
  19. system(&quot;cls&quot;)
  20. 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:

确定