帮助解决错误。该程序给出了一个关键错误,尽管一切都已经过检查并且应该是正确的。
import requests
import json
from pprint import pprint
from deep_translator import GoogleTranslator
#Выдаёт ошибку KeyError строка 22
#ввод
inp = input(">>> ") # например one pieace
morf = inp.replace(" ","+").lower()
#перевод на английский
name = GoogleTranslator(source='auto',
target='en').translate(morf)
print(name)
#подстановка в url
url=f"https://kitsu.io/api/edge/anime?filter[text]={name}"
r = requests.get(url=url)
#ответ в json
d = json.dumps(r.json())
text = json.loads(d)
#множество для уникальности записей, иначе они дублируются
data = set()
for t in text:
enTitle = text['data'][0]['attributes']['titles']['en_us']
jpTitle = text['data'][0]['attributes']['titles']['ja_jp']
title = GoogleTranslator(source='auto', target='ru').translate(enTitle)
stDate = text['data'][0]['attributes']['startDate']
edDate = text['data'][0]['attributes']['endDate']
typeA = text['data'][0]['attributes']['subtype']
desc = text['data'][0]['attributes']['synopsis']
descrip = GoogleTranslator(source='auto', target='ru').translate(desc)
img = text['data'][0]['attributes']['posterImage']['original']
ep = text['data'][0]['attributes']['episodeCount']
lenEp = text['data'][0]['attributes']['episodeLength']
#добавляем запись во множество
data.add(str(title) +"\n"+ str(jpTitle) +"\n"+ str(stDate) +"\n"+ str(edDate) +"\n"+ str(typeA) +"\n"+ str(descrip) +"\n"+ str(img) +"\n"+str(ep) +"\n"+ str(lenEp))
#печатаем оезультат
print(data)
0 个回答