TypeError: 'ChannelParticipants' object is not subscriptable

Viewed 25

Getting this error while trying to scrape chat to CSV file from telegram group. Please help!

 Traceback (most recent call last):   File
 "C:\Users\Administrator\Downloads\TelegramScraper-master\Telegram_Message_Scraper-main\m_scraper_groups.py",
 line 57, in <module>
     client.get_participants(target_group.title)   File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\sync.py",
 line 39, in syncified
     return loop.run_until_complete(coro)   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py",
 line 646, in run_until_complete
     return future.result()   File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\client\chats.py",
 line 507, in get_participants
     return await self.iter_participants(*args, **kwargs).collect()   File
 "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\requestiter.py",
 line 113, in collect
     async for message in self:   File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\requestiter.py",
 line 74, in __anext__
     if await self._load_next_chunk():   File "C:\Users\Administrator\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\telethon\client\chats.py",
 line 224, in _load_next_chunk
     participants = results[i] TypeError: 'ChannelParticipants' object is not subscriptable

Here is my code:

from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.functions.messages import GetHistoryRequest
from telethon.tl.types import PeerUser
from telethon.tl.types import InputPeerEmpty
from csv_saver import *
from auth_info import *

chats = []
last_date = None

# set the limit of members amount
chunk_size = 100000

# set limit of messages amount
while True:
    try:
        limit_msg = int(input("Enter messages count to parse "))
        break
    except:
        print("Enter integer...")
        continue

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)
        groups.append(chat)
    except:
        continue

# sort groups by alphabetic order
groups.sort(key=lambda x: x.title, reverse=False)

print('Choose a group to scrape messages 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 Messages...')

channel_entity=client.get_entity(target_group.title)
client.get_participants(target_group.title) 
posts = client(GetHistoryRequest(
    peer=channel_entity,
    limit=limit_msg,
    offset_date=None,
    offset_id=0,
    max_id=0,
    min_id=0,
    add_offset=0,
    hash=0))

post_msg = posts.messages

# save the results
all_msg = []

for m in post_msg:
    msg_lst = []
    
    try:
        if client.get_entity(PeerUser(int(m.from_id.user_id))).username is None:
            from_ = m.from_id.user_id
        else:
            from_ = client.get_entity(PeerUser(int(m.from_id.user_id))).username
    except:
        from_ = ""
        
    msg_lst.append(target_group.title)    
    msg_lst.append(from_)
    msg_lst.append(m.message.replace('\t', ' ').replace("\n", "{ENTER}"))
    msg_lst.append(m.date)
    all_msg.append(msg_lst)

all_msg = all_msg[::-1]

csv_save_group(all_msg)
0 Answers
Related