好的,让我们开始说我是一个Python完全菜鸟。
因此,我正在与Telethon合作以获取Telegram频道的整个(超过200个)成员列表。

反复尝试,我发现这段代码非常适合实现我的目标,如果不是,它仅打印前200个成员。

from telethon import TelegramClient, sync

# Use your own values here
api_id = xxx
api_hash = 'xxx'
name = 'xxx'
channel = 'xxx'

client = TelegramClient('Lista_Membri2', api_id, api_hash)
try:
client.start()
# get all the channels that I can access
channels = {d.entity.username: d.entity
        for d in client.get_dialogs()
        if d.is_channel}

# choose the one that I want list users from
channel = channels[channel]

# get all the users and print them
for u in client.get_participants(channel):
 print(u.id, u.first_name, u.last_name, u.username)

#fino a qui il codice

finally:
client.disconnect()


有人解决了吗?
谢谢!!

最佳答案

您看过Telethon文档吗?它解释说,Telegram在服务器端的限制是只能收集组中的前200名参与者。据我所知,您可以将iter_participants函数与aggressive = True一起使用来解决此问题:

https://telethon.readthedocs.io/en/latest/telethon.client.html?highlight=200#telethon.client.chats.ChatMethods.iter_participants

我以前没有使用过这个软件包,但是看起来您可以这样做:

from telethon import TelegramClient

# Use your own values here
api_id = 'xxx'
api_hash = 'xxx'
name = 'xxx'
channel = 'xxx'

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

client.start()
# get all the channels that I can access
channels = {d.entity.username: d.entity
            for d in client.get_dialogs()
            if d.is_channel}

# choose the one that I want list users from
channel = channels[channel]

# get all the users and print them
for u in client.iter_participants(channel, aggressive=True):
  print(u.id, u.first_name, u.last_name, u.username)

#fino a qui il codice
client.disconnect()

10-01 23:49