Scraping Telegram Group Members with Python and Telethon

Telegram is one of the best communications apps around the world. People usually use Telegram for managing their communities and promotions.

Startup companies or ongoing projects use Telegram for bringing audience attention to their products and services. Telegram Members are engaging with the community! This is what we all want. Engaged members will help to grow the community.

In this tutorial, you will learn how to use Telegram API extract group members.

You may also like: Scrapy, Powerful Web Scraping with Python

 

So why scraping members from Telegram Groups?

It’s a nice opportunity to get attentions from related Telegram groups. You may want to scrape members from other related groups and add them to yours. Also, you can send messages to them and start engaging (Without Spamming!)

Enough Talking. Let’s get our hands dirty with the code.

Create a Telegram App and Get Your Credentials

Go to my.telegram.org  and log in.

Click on API development tools and fill the required fields.

You can choose any name for your app. After submitting, you will receive api_id and api_hash. Save them somewhere. You will use these credentials to login to Telegram API.

 

Install Telethon

Telethon is a  great MTProto API Telegram client library written by LunamiWebs, you can check the Github page here. You can install telethon using pip:

 

Note: If you are on Linux or Mac, you might need to use sudo before pip to avoid permissions issues.

 

Create Client Object and Login

Latest version of telethon has two sync and async modules. The async module is using asyncio which is out of the scope of this article. Although you can get the same functionality using both modules but for the sake of simplicity we will use the sync module  in this tutorial.

So as the first step you need to import the sync module from Telethon library.

Then, instantiate your client object using the credentials you got before.

Next step would be connecting to telegram and checking if you are already authorized. Otherwise send an OTP code request and ask user to enter the code they received on their telegram account.

After logging in, a .session file will be created. This is a database file which makes your session persistent.

 

Listing All Telegram Groups

Create an empty list for chats and populate with the results which you get from GetDialogsRequest. You need to import two more functions:  GetDialogsRequest and  InputPeerEmpty

Note: offset_date and  offset_peer are used for filtering the chats. We are sending empty values to these parameters so API returns all chats. offset_id and limit are used for pagination. Here we are getting last 200 chats of the user.

In this tutorial, we assume that we are only interested in mega groups; so check if the megagroup attribute of the chat is True and add it to your list.

This is warped inside a try, except block avoid getting an error, because some chats do not have megagroup attribute at all. So basically we skip those using continue statement.

 

Ask User to Select a Group to Scrape Members

After listing the groups, prompt the user to input a number and select the group they want. When this code is executed it loops through every group that you stored in previous step and print it’s name starting with a number. This number is the index of that is your group list.

Ask user to enter a number associated with a group.  Then use this number as index to get the target group.

 

Get All Telegram Group Members

The last but not least step is to export all members (participants) of the Telegram group. Fortunately, there is a function for this in Telethon library which makes our job really simple.

Create an empty list of users and get members using the  get_participants  function and populate the list.

Important: Set the aggressive parameter to True otherwise you will not get more than 10k members. When aggressive is set to true, Telethon will perform an a-z search in the group’s participants and it usually extracts more than 90% of the members.

 

Store Scraped Telegram Members in a CSV File

Now use Python’s csv module to store the scraped data in a CSV file. First open a csv file in the write mode with an UTF-8 encoding. This is important because users might have non ASCII names which is very common in Telegram groups. Then create a CSV writer object and write the first row (header) in the CSV file. Finally, loop through every item in the all_participants list and write them to the CSV file.

 

Note 1: Not every user has a username. If the user has no user name the API will return None. To avoid writing None and instead of writing an empty row, check if the user has a user name; otherwise, create an empty string as the username.

Note 2: Similar to the username, some user might don’t have a first name or last name, so we are doing the same thing for name as well.

For some large groups it might take a few minutes to get the members. But finally you should see this message  Members scraped successfully.  which shows that everything worked perfectly.

So we stored username, name, user id, user hash and the group details for every user in the CSV file. You can use user id and user hash to Add Scraped Telegram Members to Your Group or Send a Message to Telegram Group Members Using Telethon. More on that in next tutorials.

 

Complete Code of Telegram Group Members Extraction Tutorial

Here is the complete executable code for this tutorial.

 

 

I speak Python!

Majid Alizadeh is a freelance developer specialized in web development, web scraping and automation. He provides high quality and sophisticated software for his clients. Beside Python he works with other languages like Ruby, PHP and JS as well.

Rating: 4.3/5. From 4 votes.
Please wait...

21 Replies to “Scraping Telegram Group Members with Python and Telethon”

  1. chats = []
    last_date = None
    chunk_size = 200
    groups=[]

    result = client(GetDialogsRequest(
    offset_date=last_date,
    offset_id=0,
    offset_peer=InputPeerEmpty(),
    limit=chunk_size,
    hash = 0
    ))
    chats.extend(result.chats)

    YOU DON’T NEED THESE LINE A SECOND TIME…

    Rating: 1.0/5. From 1 vote.
    Please wait...
    1. You are right; thanks! We have removed the repeated part.

      Rating: 5.0/5. From 1 vote.
      Please wait...
  2. I get the following error

    File “a.py”, line 13, in
    client.send_code_request(phone)
    File “C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-package
    s\telethon\sync.py”, line 54, in syncified
    return loop.run_until_complete(coro)
    File “C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base
    _events.py”, line 584, in run_until_complete
    return future.result()
    File “C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-package
    s\telethon\client\auth.py”, line 386, in send_code_request
    phone, self.api_id, self.api_hash))
    File “C:\Users\****\AppData\Local\Programs\Python\Python37-32\lib\site-package
    s\telethon\client\users.py”, line 59, in __call__
    result = await future
    telethon.errors.rpcerrorlist.FloodWaitError: A wait of 58593 seconds is required
    (caused by SendCodeRequest)

    No votes yet.
    Please wait...
    1. Hi Karina! The FloodWaitError requires you to wait an extra time; so you have to avoid it in the first place. Please check our other tutorial on adding the scraped members to your group at: https://python.gotrained.com/adding-telegram-members-to-your-groups-telethon-python/

      No votes yet.
      Please wait...
  3. Hi please am a noob can you make a video on this .
    It will be very help full
    Cause am lost i have installed the telethon on my ubuntu server
    I don know where to go to next
    I really need this a video will do

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. Hi Solomon! We are working on a video, but it takes some time. In the meanwhile try to follow the tutorial step by step and let us know if you have questions.

      No votes yet.
      Please wait...
      1. I have fixed it thanks
        Am also using the adding script but peer flood has been my major issue
        After 20-40members
        And I even increased the time for randomization to 130, 200
        Still same thing

        Also I a channel script to grow a channel will also be welcomed

        No votes yet.
        Please wait...
  4. i get this thing repeatedly

    but there is nothing in the file just the headers usernames , user id etc

    what should i do to fix it?

    No votes yet.
    Please wait...
    1. Hi Sadiq! I see you are commenting on the other tutorial of adding members. So have you fixed your issue with scraping them in the first place?

      No votes yet.
      Please wait...
  5. how can i add the users to channel , and filter their recent activity ?
    thank you very much

    No votes yet.
    Please wait...
    1. Hello! Hopefully, this helps you:
      https://github.com/LonamiWebs/Telethon/issues/781

      No votes yet.
      Please wait...
  6. Hey bro,
    It works flawlessly, my question is:
    what if i want to list channels instead of supergroups?

    No votes yet.
    Please wait...
    1. can you please explain how you do this? can you contact me on Emai i have one more question bigicxxl@hotmail.com

      No votes yet.
      Please wait...
  7. is here abybody who can give me some link where can have guide how to execute all this commands in python? because i never used python before and not understand how to do all this? is there any youtube guide… thanks

    No votes yet.
    Please wait...
    1. There are many resources that teach Python basics. Check for example the Coursera’s course “Python for Everyone”.

      No votes yet.
      Please wait...
  8. i replace only this :
    api_id = 123456
    api_hash = ‘YOUR_API_HASH’
    phone = ‘+111111111111’

    right?

    No votes yet.
    Please wait...
    1. Right.

      No votes yet.
      Please wait...
  9. so if everything is right why then this not working….. can anybody help me?

    No votes yet.
    Please wait...
  10. i have a error :
    Traceback (most recent call last):
    File “test.py”, line 18, in
    result = client(GetDialogsRequest(
    NameError: name ‘GetDialogsRequest’ is not defined

    how to solve this error?

    No votes yet.
    Please wait...
    1. Hi Sinaaa! Have you imported GetDialogsRequest first?
      from telethon.tl.functions.messages import GetDialogsRequest

      If yes, check the name of your file; it cannot be “telethon”.

      No votes yet.
      Please wait...
  11. hey, when i try to add with usernames it works fine, but with user id it is showing error releted to apihash

    No votes yet.
    Please wait...

Leave a Reply