首页 > 解决方案 > RuntimeWarning:协程“Message.download_media”从未等待过 Telethone,Python

问题描述

您好,我正在尝试使用 python 上的电话从电报组获取照片,但出现错误。我在互联网上检查它,但我无法解决我的问题。
在我的电报组上有 5 条消息

1-DENEME

2-DENEME1

3-DENEME2

4 张照片 (jpeg)

5 种不同的照片 (jpeg)

这是我的代码:

from telethon import TelegramClient, functions, types
from tqdm import tqdm
import asyncio
import os
from asyncio import run



async def main():
        async with TelegramClient(username, api_id, api_hash) as client:

            if not client.is_user_authorized():
                client.send_code_request(phone)
                try:
                    client.sign_in(phone, input('Enter the code: '))
                except SessionPasswordNeededError:
                    client.sign_in(password=input('Password: '))

            async for message in client.iter_messages(chat_id):
                print(message.sender.username, message.text)
                if message.media is not None:
                    message.download_media(message.media,"/home/Desktop/telegram/")

                    print(message.sender.username, message.media)


asyncio.run(main())

这是我的输出:

yamur@Yamur:~/Desktop/telegram$ python3 deneme.py
deneme.py:38: RuntimeWarning: coroutine 'UserMethods.is_user_authorized' was never awaited
  if not client.is_user_authorized():
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
YagmurOzden08 
deneme.py:48: RuntimeWarning: coroutine 'Message.download_media' was never awaited
  message.download_media(message.media,"/home/yamur/Desktop/telegram/")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
YagmurOzden08 MessageMediaDocument(document=Document(id=5861515192256432668, access_hash=-1448422375395726408, file_reference=b'\x04O\x8a)\r\x00\x00\x00\x11_\xab\xe4\x94\xdf\xf2\x07\x82\xe5g]\x8bS\x10\x92\xaf\xf8z\x8e\xdc', date=datetime.datetime(2020, 11, 10, 19, 35, 34, tzinfo=datetime.timezone.utc), mime_type='image/jpeg', size=246422, dc_id=4, attributes=[DocumentAttributeImageSize(w=1600, h=901), DocumentAttributeFilename(file_name='IMG_9817.JPG')], thumbs=[PhotoStrippedSize(type='i', bytes=b'\x01\x16(\xaf\xe6\xe3\xd2\x8d\xe7\xd4~T\xd0\x01\xefJS\x8e9>\x98\xa0\t"\n\xe4\xee\x03\xa5#`1\x01\x01\x1e\xa4\xd3b`\xa4\xe7#\xe9H\xf2\xf2q\xd3\xde\x99j\xddDc\xe9\x18\x14SZL\x8cc\x14P;\xc4v\xd7\x1d\x08\xa6\xb38\xfb\xcdE\x15|\xa8\xe7\xe6dfE\x1dK~T\x9eb\x8f_\xca\x8a*l\x8aL<\xe5\xfe\xe6\x7fJ(\xa2\x90\xec\x7f'), PhotoSize(type='m', location=FileLocationToBeDeprecated(volume_id=400113300938, local_id=12461), w=320, h=180, size=17740)], video_thumbs=[]), ttl_seconds=None)
YagmurOzden08 
YagmurOzden08 MessageMediaDocument(document=Document(id=5861515192256432644, access_hash=3036434682643583010, file_reference=b'\x04O\x8a)\r\x00\x00\x00\x0f_\xab\xe4\x94\x13\xb3\xa0\xbbrt=#\xfdL\x8c\x87\xf9\xf2\x18\xf4', date=datetime.datetime(2020, 11, 10, 17, 18, 23, tzinfo=datetime.timezone.utc), mime_type='image/jpeg', size=425805, dc_id=4, attributes=[DocumentAttributeImageSize(w=2048, h=1152), DocumentAttributeFilename(file_name='pp.jpg')], thumbs=[PhotoStrippedSize(type='i', bytes=b'\x01\x16(dR\xb4`\x94\xdb\xcfr3\x8a\x16\x06\x04\xac\x89\xb7+\xc56\xdco\x91W\xd7\xadX{\xa6\xdf\x97\x84\x1c\x1e3\xc1\xa9*\xf6*}\x98\x9e\x88\xd4\xaa\x8e\\ V_RkN;\x94\x9e7\xf9\x08*:\x9fz\xcf3\xc8n\xc4e\xbe]\xd8\xe9L|\xce\xda\x13\xaa\x04@\x83\xa0\xa2\x9c\xeaW\xafOQEQ\x91\x9a\xccO^\x9e\x94$\xae\x9fu\x88\x1e\x99\xa2\x8aH\xb6Y\xb7\xb9r\xcb\x11\x0b\x87p\t\xc6*\xd3\xd9F\x97\x01\xc319\xdd\x83\xd2\x8a)0D\xa7\x00\x11\x8a(\xa2\xa4g'), PhotoSize(type='m', location=FileLocationToBeDeprecated(volume_id=400120600039, local_id=10798), w=320, h=180, size=17808)], video_thumbs=[]), ttl_seconds=None)
YagmurOzden08 DENEME2
YagmurOzden08 DENEME1
YagmurOzden08 DENEME

标签: pythonpython-asynciotelegramtelethon

解决方案


你应该await它,它在async def

await message.download_media("/home/Desktop/telegram/")

推荐阅读