Answer the question
In order to leave comments, you need to log in
How to get the value of a variable from a Python function?
There is a bot script.
It has a parse() function that parses the site and finds the necessary values: date, month, weather, sunset, sunrise, etc.
I need to get the value of "date" and "month" in the function ( @bot.message_handler(commands= ['today'])
def forecast_today(message): ). How to do it?
import telebot
import urllib.request
from bs4 import BeautifulSoup
def get_html(url):
response = urllib.request.urlopen(url)
return response.read()
def parse(html):
soup = BeautifulSoup(html, features="html5lib")
div = soup.find('div', class_='tabs')
blocks = div.find('div', class_='main')
day_name = blocks.find('p', class_='day-link')
day_date = blocks.find('p', class_='date')
day_month = blocks.find('p', class_='month')
weather = blocks.find('div', class_="weatherIco").get('title')
temperature_min = blocks.find('div', class_='min')
temperature_max = blocks.find('div', class_='max')
DayLight = soup.find('div', class_='infoDaylight')
table = soup.find('table', class_='weatherDetails')
rows = table.find_all('td', class_='p4')
city = soup.find('div', class_='cityName')
city_h1 = city.find('h1')
# НУЖНЫЕ ЗНАЧЕНИЯ
date = day_date.text
month = day_month.text
humidity = rows[5].text
pressure = rows[4].text
min_t = temperature_min.text
max_t = temperature_max.text
daylight = DayLight.text.strip()
sunset = daylight[:12]
sunrise = daylight[12:].lstrip()
Name_Of_City = city_h1.text.strip()
def main():
parse(get_html(''))
token = ""
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def start_chat(message):
bot.send_message(message.chat.id, "Привет! Я могу подсказать погоду - введи /today")
@bot.message_handler(commands=['today'])
def forecast_today(message):
bot.send_message(message.chat.id, "Вы выбрали прогноз на сегодня - " + date + "/" + month + "! Введите ваш город!")
if __name__ == '__main__':
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question