Home > Software engineering >  Scraping a Public Telegram Chat (Via previewing in browser)
Scraping a Public Telegram Chat (Via previewing in browser)

Time:03-04

I'm trying to retrieve the latest message from a public chat in Telegram, specifically https://t.me/s/ukrainenowenglish.

Would this be achievable with simply using the URL? Or should would I be further ahead to utilize the Telegram API? The structure of Telegram in Web Browser is throwing me off, so any sort of ideas on where I should be looking would be very much appreciated.

CodePudding user response:

You can just do something like this

import requests
from bs4 import BeautifulSoup

URL = "https://t.me/s/ukrainenowenglish"
r = requests.get(URL)

soup = BeautifulSoup(r.content, 'html5lib')
messages = soup.find_all("div", class_="tgme_widget_message_text")
lastmessage = messages[len(messages)-1].text

print(lastmessage)

You may need to install those three libraries

pip install requests
pip install html5lib
pip install bs4
  • Related