C
C
ceyolic2021-10-20 20:51:04
Bots
ceyolic, 2021-10-20 20:51:04

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

1 answer(s)
W
WolfInChains, 2021-10-20
@ceyolic

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 question

Ask a Question

731 491 924 answers to any question