Scrape Telegram Group Messages

In this article, I am going to show you how to scrape Telegram group messages using Python. The program is going to help you if you want to scrape all the messages and save them in a CSV or Excel file to do some analysis. For example, if you want to search how many times certain words are being used in the group.

Installing Telethon

We need to install Telethon Library in order to scrape members from any group. You can install Telethon by using pip and running the below commands.

pip install Telethon

Installing Pandas

We also need to install the Pandas library as we will be saving all the scraped messages in a CSV or Excel File. You can install Pandas by running the below command.

pip install pandas

Getting Telegram API ID and API Hash

You will need to get a Telegram API ID and API Hash in order to programmatically access your Telegram account. You will need to register to My Telegram Org in order to get these details.

Python Program to Scrape Telegram Group Messages

from telethon.sync import TelegramClient
import datetime
import pandas as pd

import configparser
config = configparser.ConfigParser()"telethon.config")

api_id = config["telethon_credentials"]["api_id"]
api_hash = config["telethon_credentials"]["api_hash"]

chats = ['cryptodubai7', 'Verasity_Official']

client =  TelegramClient('test', api_id, api_hash)

df = pd.DataFrame()

for chat in chats:
    with TelegramClient('test', api_id, api_hash) as client:
        for message in client.iter_messages(chat, , reverse=True):
            data = { "group" : chat, "sender" : message.sender_id, "text" : message.text, "date" :}

            temp_df = pd.DataFrame(data, index=[1])
            df = df.append(temp_df)

df['date'] = df['date'].dt.tz_localize(None)

df.to_excel("C:\\crypto\\data_{}.xlsx".format(, index=False)

Learn to Scrape Telegram Group Members

%d bloggers like this: