如何使用 telethon 将图像下载到本地 PC

Mig*_*Mag 5 python telegram telethon

我看过所有询问有关电视马拉松的条目,但没有一个回答我的问题。

我想要实现什么?我想将图像从 telegram 频道下载到我的电脑中

到目前为止我做了什么?我能够创建一个客户端并读取包含图像的消息。但是,在 download_media 之后,我不知道文件在哪里或如何继续。

注意:我不想使用任何 BOT。

这是我的代码:

import configparser #read API credentials from a config file
import json         # dump data into JSON formatted files.

from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError

from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.types import (PeerChannel)

from telethon.tl.functions.messages import (GetHistoryRequest)

from telethon import utils

#**************** CREATE TELEGRAM CLIENT ******************************
# Reading Configs
config = configparser.ConfigParser()
config.read("E:\TelegramBOT\config.ini")

# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']

api_hash = str(api_hash)

phone = config['Telegram']['phone']
username = config['Telegram']['username'] 

# Log in to Telegram and create a client object to use for getting data
    # Create the client and connect
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
    # Ensure you're authorized
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 def main():
    # You can print the message history of any chat:
    async for message in client.iter_messages('<CHANNEL>'):
        print(message.sender.username, message.text)
        if not message.media == False:
            message.download_media(message.media, "E:\TelegramBOT\Tips")
Run Code Online (Sandbox Code Playgroud)

最后几行输出:

<coroutine object Message.download_media at 0x00000238F1E657C8>
Run Code Online (Sandbox Code Playgroud)

所以问题是:如何保存/查看/下载该图像?

小智 3

我不是这方面的专家,但我通过几次尝试得到了这个结果。

await client.download_media(message.media,"/save_path")
Run Code Online (Sandbox Code Playgroud)

如果您不输入 save_path,它会将其保存在您当前的目录中。

希望它能解决您的问题。

  • 这仅下载拇指。有没有办法下载完整的图片? (2认同)