Answer the question
In order to leave comments, you need to log in
Telegram Bot saves the cryptocurrency exchange rate incorrectly. Why is this happening?
I am writing a telegram bot and everything seems to be working for me, but alas, not as it should be. In short, I will say what kind of bot it is. I made a telegram bot that, using a parser, enters the site, reads the bitcoin rate in dollars and sends it to a person after a certain time and compares whether the bitcoin rate has changed. If yes, then it sends a new course value to the person. But I apparently did not correctly save the bitcoin rate so that the bot compares and sends, since it sends alternating values (I attached the screen below).
import telebot
import requests
import time
from bs4 import BeautifulSoup
token = ""
channel_id = ""
bot = telebot.TeleBot(token)
@bot.message_handler(content_types=['text'])
def commands(message):
#bot.send_message(channel_id, message.text)
if message.text == "Курс":
#bot.send_message(channel_id, "Hello")
back_post_id = None
while True:
post_text = parser(back_post_id)
back_post_id = post_text[1]
if post_text[0] != None:
bot.send_message(channel_id, post_text[0])
time.sleep(1)
else:
bot.send_message(message.from_user.id, "Я тебя не понимаю. Напиши Курс")
def parser(back_post_id):
URL = "https://currency.com/ru/btc-to-usd?utm_course=rbc&utm_campaign=quote"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "lxml")
post = soup.find("b", class_='i-price-buy').text
post_id = post
if post_id != back_post_id:
post = soup.find("b", class_='i-price-buy').text
return f"{post}", post_id
else:
return None, post_id
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
It makes no sense to compare once a second - the market is alive and the exchange rate changes very often, this is not a dollar / euro on cbr.ru.
According to the subject - on the site there is some kind of cached value, which the parser receives. (you can easily check it just by looking at the source https://currency.com/en/btc-to-usd?utm_course=rbc&... )
On the site, the course is updated via a websocket, so it needs to be parsed. We subscribe to the desired cryptocurrency and rejoice
import websocket
import _thread
import json
def on_message(ws, message):
data = json.loads(json.loads(message)['data'])
print(data['bid'], data['ask'])
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
def run(*args):
ws.send(json.dumps({"event":"pusher:subscribe","data":{"channel":"45076691096786116"}}))
_thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(False)
ws = websocket.WebSocketApp("wss://prod-pusher.backend-capital.com/app/MvtsstCbm?protocol=7&client=js&version=4.2.2&flash=false",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question