使用 Telethon for Telegram 时如何更正“'coroutine' 对象没有属性'data'”错误?

DDD*_*lan 5 python python-3.x telegram telethon

我正在尝试用 Python 编写一个简单的东西来自动从 Telegram 上的频道下载一些媒体。为此,我正在使用 Telethon。

我不断收到一个我无法解决的错误,我不明白原因。

“‘协程’对象没有‘数据’属性”

我也尝试过使用 asyncio,但是没有用。下面是我最新的代码

# In[1]:

import asyncio
loop = asyncio.get_event_loop()
import telethon.sync
from telethon import TelegramClient
from telethon.sync import TelegramClient

loop = asyncio.get_event_loop()


# In[2]: 

api_id = #MyAPIID
api_hash = 'TheHash'
phone_number = '+34xxxxx'
channel_username = 'meanwhileinromania'


# In[3]: 

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

client.start()




# In[4]:DOWNLOAD


msgs = client.get_messages(channel_username, limit=100)
for msg in msgs.data:
    if msg.media is not None:
        client.download_media(message=msg)
Run Code Online (Sandbox Code Playgroud)

我得到 [3]:

<coroutine object AuthMethods._start at 0x00000190D413F9C8>
Run Code Online (Sandbox Code Playgroud)

并在 [4]:

    C:\Users\user1\AppData\Local\Programs\Python\Python37\lib\site-packages\ipykernel_launcher.py:3: RuntimeWarning: coroutine 'MessageMethods.get_messages' was never awaited
  This is separate from the ipykernel package so we can avoid doing imports until
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
      2 
      3 msgs = client.get_messages(channel_username, limit=100)
----> 4 for msg in msgs.data:
      5     if msg.media is not None:
      6         client.download_media(message=msg)

AttributeError: 'coroutine' object has no attribute 'data'
Run Code Online (Sandbox Code Playgroud)

Joh*_*ohn 12

如果msgs是协程,则需要等待。很可能循环之前的行应该是msgs = await client.get_messages(channel_username, limit=100)