Недавно начал разбираться с ботами на пайтоне. Нашел пример как парсить погоду. Добавил код к себе, но замучила ошибка.
Мой код:
import telebot
import requests
from bs4 import BeautifulSoup as BS
r = requests.get('https://sinoptik.ua/погода-харьков')
html = BS(r.content, 'html.parser')
bot = telebot.TeleBot('1113481836:AAGB_KMA0glwKctU2sKiH6bLS1zTyYYavlo')
keyboard1 = telebot.types.ReplyKeyboardMarkup()
keyboard1.row(' Rozklad', '⚙️ Кнопка4','Погода')
keyboard1.row(' Наш сайт', ' Зворотній зв`язок')
for el in html.select('#content'):
t_min = el.select('.temperature .min')[0].text
t_max = el.select('.temperature .max')[0].text
text = el.select('.wDescription .description')[0].text
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Привіт, '+ message.from_user.username)
@bot.message_handler(content_types=['text'])
def send_text(message):
# global t_min
if message.text == ' Rozklad':
bot.send_message(message.chat.id, 'Обери курс:', reply_markup=kurs)
# if message.text == 'Погода':
# global tmin
# bot.send_message(message.chat.id, tmin, reply_markup=kurs)
if message.text == 'Погода':
bot.send_message(message.chat.id, "Привет, погода на сегодня \n" +
t_min + ', ' + t_max + '\n' + text)
if __name__ == '__main__':
bot.polling(none_stop=True)
После запуска и нажатия на погоду выбивает ошибки:
2020-06-21 08:07:50,838 (util.py:68 WorkerThread2) ERROR - TeleBot: "NameError occurred, args=("name 't_min' is not defined",)
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "C:/Users/spider2007/PycharmProjects/untitled/sample/ldgik.py", line 48, in send_text
t_min + ', ' + t_max + '\n' + text)
NameError: name 't_min' is not defined
"
Traceback (most recent call last):
File "C:/Users/spider2007/PycharmProjects/untitled/sample/ldgik.py", line 69, in
bot.polling(none_stop=True)
File "C:\Python37\lib\site-packages\telebot\__init__.py", line 415, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C:\Python37\lib\site-packages\telebot\__init__.py", line 439, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Python37\lib\site-packages\telebot\util.py", line 111, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Python37\lib\site-packages\six.py", line 703, in reraise
raise value
File "C:\Python37\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "C:/Users/spider2007/PycharmProjects/untitled/sample/ldgik.py", line 48, in send_text
t_min + ', ' + t_max + '\n' + text)
NameError: name 't_min' is not defined
Process finished with exit code 1