【问题标题】:How to solve the error 'coroutine' object has no attribute 'users'?如何解决错误“协程”对象没有属性“用户”?
【发布时间】:2022-02-22 16:56:33
【问题描述】:

我想使用 Telethon 来Retrieving all chat members

这是我的代码:

from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from time import sleep
api_id = 134565
api_hash = 'xxxxxx'
client = TelegramClient(None, api_id, api_hash)
 
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))
offset = 0
limit = 100
all_participants = []

while True:
    participants = client(GetParticipantsRequest(
        channel, ChannelParticipantsSearch(''), offset, limit,
        hash=0
    ))
    if not participants.users:
        break
    all_participants.extend(participants.users)
    offset += len(participants.users)
Note

代码出现以下错误: 我试试这个Q/A,但它没有关于错误的足够信息。

<module>
      19         hash=0
      20     ))
 ---> 21     if not participants.users:
      22         break
      23     all_participants.extend(participants.users)
 
 AttributeError: 'coroutine' object has no attribute 'users'

如何解决这个问题?

【问题讨论】:

  • 您需要提供更详细的错误信息
  • 首先,您应该提供可重现的代码,例如您缺少从 Telethon 导入 TelegramClient。与我想你想做的相关,是检索聊天中的参与者,你可以使用这种方法来代替,get_participants

标签: python telethon


【解决方案1】:

python中的协程对象用于异步程序,详情可以阅读 Coroutines and Tasks.

在您的代码中,引发错误'coroutine' object has no attribute 'users',可能是由于您调用了异步 API 但没有等待它。

你可以在office网站看到一个例子,例子使用了async/await

【讨论】:

  • 它不起作用。你能用这个解决方案修改我的代码吗?
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 2019-09-08
  • 2020-10-25
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多