问题
I am trying to code a simple thing in Python to automatically download some media from a Channel on Telegram. I am using Telethon for this.
I keep getting an error that I cannot solve and for which I do not understand the reason.
"'coroutine' object has no attribute 'data'"
I have tried to use asyncio as well, but it didn't work. Here below my latest code
# 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)
I get for [3]:
<coroutine object AuthMethods._start at 0x00000190D413F9C8>
and at [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'
回答1:
If msgs
is a co-routine, it needs to be awaited. So likely the line before the loops should be msgs = await client.get_messages(channel_username, limit=100)
来源:https://stackoverflow.com/questions/57147419/how-to-correct-coroutine-object-has-no-attribute-data-error-when-using-te