Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi i try to scrape people in public group , the group is big 7000 people i use this script

client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))
 
 
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)
 
for chat in chats:
    try:
        if chat.megagroup== True:
            groups.append(chat)
    except:
        continue
 
print('Choose a group to scrape members from:')
i=0
for g in groups:
    print(str(i) + '- ' + g.title)
    i+=1
 
g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]
 
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=False, limit=5660)
 
print('Saving In file...')
with open("erfan4lx.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])      
print('Members scraped successfully.')
print ('''

if i set limit=5660 work but after this not go and return error , is possible fix it?

What I have tried:

my error
<pre><pre>File "erfan4lx.py", line 69, in <module>
    all_participants = client.get_participants(target_group, aggressive=False, limit=5660)
  File "/home/giulio/.local/lib/python3.8/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/giulio/.local/lib/python3.8/site-packages/telethon/client/chats.py", line 503, in get_participants
    return await self.iter_participants(*args, **kwargs).collect()
  File "/home/giulio/.local/lib/python3.8/site-packages/telethon/requestiter.py", line 113, in collect
    async for message in self:
  File "/home/giulio/.local/lib/python3.8/site-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/home/giulio/.local/lib/python3.8/site-packages/telethon/client/chats.py", line 223, in _load_next_chunk
    participants = results[i]
Posted
Comments
Richard MacCutchan 5-Dec-22 3:59am    
The actual error message seems to be missing.
GiulioRig 5-Dec-22 5:47am    
sorry not have copyed the last row
TypeError: 'ChannelParticipants' object is not subscriptable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900