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.

 

 

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

46 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: 3.4/5. From 16 votes.
    Please wait...
    1. You are right; thanks! We have removed the repeated part.

      Rating: 3.7/5. From 6 votes.
      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)

    Rating: 4.6/5. From 14 votes.
    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/

      Rating: 2.8/5. From 5 votes.
      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: 4.1/5. From 9 votes.
    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.

      Rating: 3.3/5. From 8 votes.
      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

        Rating: 2.7/5. From 3 votes.
        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?

    Rating: 2.5/5. From 2 votes.
    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?

      Rating: 3.0/5. From 4 votes.
      Please wait...
  5. how can i add the users to channel , and filter their recent activity ?
    thank you very much

    Rating: 3.4/5. From 5 votes.
    Please wait...
    1. Hello! Hopefully, this helps you:
      https://github.com/LonamiWebs/Telethon/issues/781

      Rating: 3.0/5. From 4 votes.
      Please wait...
  6. Hey bro,
    It works flawlessly, my question is:
    what if i want to list channels instead of supergroups?

    Rating: 2.3/5. From 3 votes.
    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

      Rating: 2.5/5. From 2 votes.
      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

    Rating: 3.0/5. From 3 votes.
    Please wait...
    1. There are many resources that teach Python basics. Check for example the Coursera’s course “Python for Everyone”.

      Rating: 2.3/5. From 4 votes.
      Please wait...
  8. i replace only this :
    api_id = 123456
    api_hash = ‘YOUR_API_HASH’
    phone = ‘+111111111111’

    right?

    Rating: 3.0/5. From 2 votes.
    Please wait...
    1. Right.

      Rating: 3.5/5. From 2 votes.
      Please wait...
  9. so if everything is right why then this not working….. can anybody help me?

    Rating: 2.5/5. From 2 votes.
    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?

    Rating: 1.0/5. From 1 vote.
    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”.

      Rating: 2.5/5. From 2 votes.
      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

    Rating: 3.7/5. From 3 votes.
    Please wait...
  12. Hello sir… I have a problem in this code… Please can you fix it..

    Rating: 1.0/5. From 2 votes.
    Please wait...
    1. Could you please elaborate on the problem, or you can check other comments.

      Rating: 2.5/5. From 2 votes.
      Please wait...
  13. client.get_participants(target_group, aggressive=True)
    is generate WRONG access_hash….

    the correct access_hash is from : client.get_input_entity(user[‘username’])

    go try your self, the access_hash is different, and the one from “client.get_participants” cannot be used to add member to group because of wrong access_hash….

    any clue about this one?

    Rating: 4.0/5. From 4 votes.
    Please wait...
  14. hello mate , i am getting this error , what is the index range ( i mean which number i should give here)
    File “C:\python programs\mscraper.py”, line 45, in
    target_group=groups[int(g_index)]
    IndexError: list index out of range

    Rating: 3.0/5. From 10 votes.
    Please wait...
  15. For scrap the usernames and id from groups..admin only can sarap? Or just memnme of group is sufficient?
    Because i am not admin just member of group.

    Rating: 2.7/5. From 3 votes.
    Please wait...
    1. Hello, do you need to be an admin to scrap the members usernames ?

      Rating: 2.7/5. From 3 votes.
      Please wait...
  16. How to fix it?
    Traceback (most recent call last):
    File “username.py”, line 1, in
    from telethon.sync import TelegramClient
    File “C:\Program Files (x86)\Python37-32\telethon\__init__.py”, line 1, in
    from .client.telegramclient import TelegramClient
    File “C:\Program Files (x86)\Python37-32\telethon\client\__init__.py”, line 12, in
    from .telegrambaseclient import TelegramBaseClient
    File “C:\Program Files (x86)\Python37-32\telethon\client\telegrambaseclient.py”, line 9, in
    from ..crypto import rsa
    File “C:\Program Files (x86)\Python37-32\telethon\crypto\__init__.py”, line 8, in
    from .authkey import AuthKey
    File “C:\Program Files (x86)\Python37-32\telethon\crypto\authkey.py”, line 7, in
    from ..extensions import BinaryReader
    File “C:\Program Files (x86)\Python37-32\telethon\extensions\__init__.py”, line 6, in
    from .binaryreader import BinaryReader
    File “C:\Program Files (x86)\Python37-32\telethon\extensions\binaryreader.py”, line 9, in
    from ..errors import TypeNotFoundError
    File “C:\Program Files (x86)\Python37-32\telethon\errors\__init__.py”, line 15, in
    from .rpcerrorlist import *
    ModuleNotFoundError: No module named ‘telethon.errors.rpcerrorlist’

    Rating: 1.7/5. From 6 votes.
    Please wait...
    1. Please make sure you update/install your Telethon library.

      Rating: 3.0/5. From 2 votes.
      Please wait...
  17. Hey How do i input my users.csv(scrapped from groups) to actually read it
    I’m not sure where to put my file name . I get error input_file = sys.argv[1]
    Index error : list index out of range.

    Rating: 3.0/5. From 2 votes.
    Please wait...
    1. I believe this question is about another tutorial; we answered it in the comments of the tutorial: Adding Telegram Group Members to Your Groups Using Telethon

      Rating: 3.0/5. From 2 votes.
      Please wait...
  18. I keep getting “NameError: name ‘client’ is not defined” I’m very new to this. Am I putting all the code in at once or one at a time? Im so confused…

    Rating: 3.5/5. From 2 votes.
    Please wait...
  19. which number i have to enter Please anyone tell me

    Choose a group to scrape members from:
    Enter a Number:

    Rating: 3.7/5. From 3 votes.
    Please wait...
    1. Hi Raj! Your Telegram number.

      Rating: 3.0/5. From 2 votes.
      Please wait...
    2. you have a list like that right?
      1 – “telegram group name wow”
      2 – “telegram group name lol”
      3 – “telegram group name ok”
      4 – “telegram group name please”
      5 – “telegram group name thank”
      6 – “telegram group name you”

      so if you want to scrape from the third one “telegram group name ok” you should press 3

      Rating: 2.8/5. From 4 votes.
      Please wait...
  20. How do we get the information of recent activity of the users of that group?

    Rating: 3.0/5. From 2 votes.
    Please wait...
    1. Jamie, check this file and search for UserStatus

      Rating: 2.3/5. From 3 votes.
      Please wait...
  21. hi it works and thnx but what should i do if my groups name have Non-BMP character? TK does not support them.

    Rating: 3.0/5. From 2 votes.
    Please wait...
    1. Saman, do you use Python 3?

      Rating: 4.0/5. From 1 vote.
      Please wait...
  22. Hey GoTrained my man!!!

    help me out please

    what if i want to scrape a channel which im not an admin at? possible?

    thanks in advance

    Rating: 3.9/5. From 8 votes.
    Please wait...
  23. 1 more question

    Rating: 1.0/5. From 2 votes.
    Please wait...
  24. 1 more question, how can i make the script show hebrew text? its all symbols and unreadable text if the group name is in hebrew…. THANKS AGAIN

    Rating: 1.0/5. From 1 vote.
    Please wait...
  25. For python 3.7 and telethon v1.8, change to “client.connect()” to “client.start()”

    Rating: 3.0/5. From 2 votes.
    Please wait...
  26. Hi. I get this error with target_group=groups[int(g_index)]

    Traceback (most recent call last):
    File “”, line 1, in
    ValueError: invalid literal for int() with base 10: ”

    How can I solve it?

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. I made a mistake and caused this error.
      The code works like a charm!
      Manoon Majid

      Rating: 2.0/5. From 2 votes.
      Please wait...
  27. nice working like a charm!Maybe a last online or only online members scrape will be nice !

    Rating: 3.7/5. From 3 votes.
    Please wait...

Comments are closed.