如何更正“协程"对象没有属性“数据"使用Telethon进行电报时出错? [英] How to correct " 'coroutine' object has no attribute 'data'" Error when using Telethon for Telegram?
问题描述
我正在尝试用Python编写一个简单的代码,以自动从Telegram的频道下载某些媒体.我正在为此使用Telethon.
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'"
我也尝试过使用asyncio,但是没有用. 在我最新的代码下面
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)
我得到[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 = await client.get_messages(channel_username, limit=100)
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)
这篇关于如何更正“协程"对象没有属性“数据"使用Telethon进行电报时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!