Telegram Bot Updater Issue

A Telegram bot’s updater malfunctions while retrieving Yahoo Finance index prices. Revised implementation is provided:

import httpx
from bs4 import BeautifulSoup
from telegram.ext import Updater, CommandHandler

def retrieve_price(ticker):
    url = f"https://finance.yahoo.com/quote/{ticker}"
    resp = httpx.get(url)
    soup = BeautifulSoup(resp.text, "html.parser")
    price_span = soup.find("span", {"data-reactid": "14"})
    return price_span.get_text() if price_span else "N/A"


def price_alert(update, context):
    indices = {"Dow": "^DJI", "SP500": "^GSPC"}
    message = "\n".join(f"{name}: {retrieve_price(symbol)}" for name, symbol in indices.items())
    context.bot.send_message(chat_id=update.effective_chat.id, text=message)

if __name__ == "__main__":
    BOT_TOKEN = "YOUR_NEW_TOKEN"
    updater = Updater(BOT_TOKEN, use_context=True)
    updater.dispatcher.add_handler(CommandHandler("price", price_alert))
    updater.start_polling()
    updater.idle()

hey, try checking if yahoo changed the html structure - maybe the data-reactid isn’t valid anymore. also, add headers to your request in case it’s blocked. give that a shot!

The issue might also be related to the inherent unreliability of web scraping in this context. I faced a similar problem and found that scraping Yahoo Finance is often affected by changes in their HTML layout. In my case, switching to using a dedicated API not only made the data retrieval more stable but also improved performance and error handling. If an API is not an option, introducing more robust parsing methods and proper error logging could alleviate some issues caused by missing or moved elements on the page.

In my experience, relying solely on static page scraping for Yahoo Finance can lead to unpredictable issues, especially when the site uses dynamic content loading. I once had a similar problem and resolved it by switching to a headless browser solution which ensured that the JavaScript-rendered content was fully loaded before parsing it with BeautifulSoup. This method proved to be more reliable and allowed for better error handling. Additionally, I made sure to add robust checks for HTTP response codes and timeouts to secure the process in case of connectivity issues.