Answer the question
In order to leave comments, you need to log in
How to make that the data after each request is updated?
A telegram bot that tells the current value of the instrument. It is necessary to make sure that after each request in the telegram, the data is updated. At the moment it prints out the price that was the last time I ran the code. Thanks in advance.
import telebot
import requests
from bs4 import BeautifulSoup
url = ' https://en.investing.com/commodities/gold '
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
gold = soup.find ('span', class_='instrument-price_last__KQzyA')
print(gold.text)
bot = telebot.TeleBot('bot token here')
@bot.message_handler(commands=['gold'])
def echo_all(message):
bot.send_message(message.chat.id, gold.text + '$')
bot.infinity_polling()
Answer the question
In order to leave comments, you need to log in
Well, put the code with getting the data into a function with the execution of the gold command
import telebot
import requests
from bs4 import BeautifulSoup
bot = telebot.TeleBot('тут токен бота')
@bot.message_handler(commands=['gold'])
def echo_all(message):
url = 'https://ru.investing.com/commodities/gold'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
gold = soup.find('span', class_='instrument-price_last__KQzyA')
print(gold.text)
bot.send_message(message.chat.id, gold.text + '$')
bot.infinity_polling()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question