Tutorial
How to Scrape Telegram Channels and Messages
Learn how to scrape Telegram channels, groups, and messages using Python. Covers Telethon, public channel scraping, and data extraction techniques.
Telegram is a rich source of data for market research, OSINT, and content analysis. Unlike most social platforms, Telegram offers relatively open access to public channel data. Here is how to scrape it.
Method 1: Telethon (Recommended)
Telethon is the best Python library for Telegram scraping. You need a Telegram API ID and hash from https://my.telegram.org.
pip install telethon
from telethon import TelegramClient
import asyncio
API_ID = "YOUR_API_ID" # From my.telegram.org
API_HASH = "YOUR_API_HASH" # From my.telegram.org
async def scrape_channel(channel_username, limit=100):
async with TelegramClient("session", API_ID, API_HASH) as client:
channel = await client.get_entity(channel_username)
messages = []
async for message in client.iter_messages(channel, limit=limit):
messages.append({
"id": message.id,
"date": str(message.date),
"text": message.text,
"views": message.views,
"forwards": message.forwards,
"replies": message.replies.replies if message.replies else 0
})
return messages
data = asyncio.run(scrape_channel("telegram_channel_name", limit=200))
for msg in data[:5]:
print(f"[{msg['date']}] {msg['text'][:100]}")
print(f" Views: {msg['views']}, Forwards: {msg['forwards']}")
Method 2: Scraping Public Channels via Web
Public Telegram channels have a web preview at t.me/s/channel_name.
import requests
from bs4 import BeautifulSoup
url = "https://t.me/s/channel_name"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
messages = soup.find_all("div", class_="tgme_widget_message_wrap")
for msg in messages:
text_el = msg.find("div", class_="tgme_widget_message_text")
views_el = msg.find("span", class_="tgme_widget_message_views")
if text_el:
print(f"Text: {text_el.get_text()[:100]}")
print(f"Views: {views_el.get_text() if views_el else 'N/A'}")
print("---")
Scraping Channel Members
from telethon import TelegramClient
import asyncio
async def get_members(channel_username):
async with TelegramClient("session", API_ID, API_HASH) as client:
channel = await client.get_entity(channel_username)
members = []
async for user in client.iter_participants(channel, limit=500):
members.append({
"id": user.id,
"username": user.username,
"first_name": user.first_name,
"last_name": user.last_name
})
return members
# Note: This only works for groups, not broadcast channels
members = asyncio.run(get_members("group_name"))
print(f"Scraped {len(members)} members")
Downloading Media
async def download_media(channel_username, limit=50):
async with TelegramClient("session", API_ID, API_HASH) as client:
channel = await client.get_entity(channel_username)
async for message in client.iter_messages(channel, limit=limit):
if message.media:
path = await message.download_media("./downloads/")
print(f"Downloaded: {path}")
Important Notes
- Rate limits, Telegram has strict rate limits. Add delays between requests (1-2 seconds).
- Account bans, Aggressive scraping can get your Telegram account banned. Use dedicated accounts.
- Privacy, Only scrape public channels and groups. Scraping private channels without consent violates Telegram's ToS.
- API limits, Telethon is limited to about 200 messages per request batch.
Telegram scraping with Telethon is one of the more straightforward social media scraping tasks since Telegram provides official API access for public data.