英文:
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+" 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)
答案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+" 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论