python - 如何获取 Telegram channel (超过 200 个)成员的列表

标签 python telegram telethon

好吧,让我们先说我是一个 Python 菜鸟。 因此,我正在使用 Telethon 获取 Telegram channel 的完整(超过 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()

有人有解决办法吗? 谢谢!!

最佳答案

您看过电视节目文档了吗?它解释说 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()

关于python - 如何获取 Telegram channel (超过 200 个)成员的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54095191/

相关文章:

python - 无法找到模块,即使它已安装并且可以运行

java - Telegram 机器人向特定人员发送私有(private)消息

api - 错误 Telegram ID 和哈希 "Incorrect app name&#33"

python - 启动客户端时,代码要求电话/机器人 token

python - 在 Heroku 上运行 Telethon

python-3.x - telethon.errors.rpcerrorlist.FloodWaitError : A wait of 41548 seconds is required (caused by InviteToChannelRequest)

python 轻松将 unicode 写入文件?

python - pyyaml 和 yaml 子级别

python - 从 numpy 数组的每一列中选择随机元素

python - 如何使用 Telethon 获取 Telegram 私有(private) channel ID