Ошибка при data['main']['temp'] Openweather
import time #Імпортуємо бібліотеку тайм, щоб потім зробити затримку
import sqlite3
import requests
import json
import telebot
from telebot import types
# Replace 'YOUR_BOT_TOKEN' with your actual bot token
bot = telebot.TeleBot('6k')
name = None
API='5d'
@bot.message_handler(commands=['weather'])
def wetn(message):
bot.send_message(message.chat.id, 'Зараз я тобі допоможу з погодою. Стоп а яку місто? ?️')
@bot.message_handler(content_types='text')
def get_weather(message):
city = message.text.strip().lower()
res = requests.get(f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API}&units=metric')
data = json.loads(res.text)
temp = data['main']['temp']
wind = data['wind']['speed']
cl = data['clouds']['all']
Ошибка:
Traceback (most recent call last):
File "c:\Users\Саша Стефанюк\Desktop\My\MyfirstBot\main.py", line 188, in <module>
bot.polling(none_stop=True)#Робимо код нескінченним
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\__init__.py", line 1043, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\__init__.py", line 1118, in __threaded_polling
raise e
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\__init__.py", line 1074, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\util.py", line 147, in raise_exceptions
raise self.exception_info
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\util.py", line 90, in run
task(*args, **kwargs)
File "C:\Users\Саша Стефанюк\AppData\Local\Programs\Python\Python312\Lib\site-packages\telebot\__init__.py", line 6801, in _run_middlewares_and_handler
result = handler['function'](message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Саша Стефанюк\Desktop\My\MyfirstBot\main.py", line 22, in get_weather
temp = data['main']['temp']
~~~~^^^^^^^^
KeyError: 'main'