S
S
spider20072020-06-21 08:20:33
Python
spider2007, 2020-06-21 08:20:33

Telegram bot in python: error variable is not defined?

Recently I started to deal with bots on Python. Found an example of how to parse the weather. I added the code to myself, but the error tortured me.
My code:

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)


After starting and clicking on the weather, it throws errors:
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

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2020-06-21
@sergey-gornostaev

Understand the basics of the language first. Read some textbook.

D
Diatomiccoder, 2020-06-21
@Diatomiccoder

The error says that there is no t_min variable. It only surrenders within the cycle. Those. most likely html.select(...) returns an empty list (or its own empty iterable object).
You can try to declare variables t_min, t_max, text (for example t_min = "" ) before the loop, and the error should disappear

N
Nikita Verkhoglyad, 2020-06-22
@Cyclops

Study the page you are parsing first.
The elements you are looking for have only one class, and you are looking for them in two classes.
And the element with the class "wDescription description"does not exist at all.
You need to get the element of the class first "temperature", and then pull out the elements with the temperature from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question