python - 如何更正 "'协程'对象没有属性 'data'“使用Telethon for Telegram时出错?

标签 python python-3.x telegram telethon

我正在尝试用 Python 编写一个简单的代码,以自动从 Telegram 上的 channel 下载一些媒体。我正在为此使用 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)

我得到[3]:

<coroutine object AuthMethods._start at 0x00000190D413F9C8>

以及[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'

最佳答案

如果msgs是一个协同例程,则需要等待它。因此循环之前的行可能应该是 msgs = wait client.get_messages(channel_username, limit=100)

关于python - 如何更正 "'协程'对象没有属性 'data'“使用Telethon for Telegram时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57147419/

相关文章:

python - 运行Pandas配置文件报告时出错

python - Kernprof (line_profiler) : how to output result as text instead of a binary file

python - 如何在 python-telegram-bot 包装器中使用 ForceReply

python - 通过 python 向 Telegram Bot 发送文本消息

python - numpy 溢出

Python:在 Windows 7 上使用 _winreg 编辑注册表值时出现 WindowsError

python - 了解tensorflow中tf.slice的参数以及为什么我无法更改它

c# - 保存发送给机器人的用户消息并将完成的表格发送给其他用户

python - 连接两个 PySpark 数据帧

python - 导入错误 : No module named 'paramiko'