python - 如何使用 Telethon 将图像下载到我的本地 PC

标签 python telegram telethon

我已经看到所有关于 Telethon 的条目,但没有一个回答我的问题。

我想达到什么目标?
我想从 Telegram channel 下载图像到我的电脑

到目前为止我做了什么?
我能够创建一个客户端并阅读包含图像的消息。但是,在我 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")

最后几行输出:
<coroutine object Message.download_media at 0x00000238F1E657C8>

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

最佳答案

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

await client.download_media(message.media,"/save_path")
如果您不输入 save_path,它会将其保存在您的当前目录中。
希望它能解决你的问题。

关于python - 如何使用 Telethon 将图像下载到我的本地 PC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62391946/

相关文章:

python - Django:显示管理站点的换行符?

java - 如何更改 Telegram 验证短信内容

bots - 如何将 Telegram channel 的成员转移到另一个 Telegram channel

python - 在 CentOS 上运行 Python 脚本作为后台任务

python-asyncio - 运行时错误: Event loop is closed after main coroutine is completed

python - 在 Python 中将鼠标光标悬停在某物上时显示消息

python - 如何将页面源代码中的json解码为字典?

python - 无法通过 instabot 将一些照片上传到 instagram

xpath - <a> 不支持元素 <img>

python - 有没有办法知道使用 telethon 的 telegram post 照片的 url?