你好,我正在尝试使用python上的Telethone从电报组中获取照片,但是我遇到了错误。我在互联网上检查过,但无法解决问题。
我的电报组中有5条消息
1-DENEME
2-DENEME1
3-DENEME2
4-SOME PHOTO(jpeg)
5-DIFFRENT照片(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
答案 0 :(得分:2)
您应该await
,它在async def
中:
await message.download_media("/home/Desktop/telegram/")