gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:45 26 4
gpt4 key购买 nike

好吧,让我们先说我是一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com