【问题标题】:How to download images to my local PC using telethon如何使用 Telethon 将图像下载到我的本地 PC
【发布时间】:2020-06-15 15:45:54
【问题描述】:

我已经看到所有关于电视马拉松的条目,但没有一个回答我的问题。

我想达到什么目标? 我想从电报频道下载一张图片到我的电脑中

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

标签: python telegram telethon


【解决方案1】:

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

await client.download_media(message.media,"/save_path")

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

希望它能解决你的问题。

【讨论】:

  • 这只会下载拇指。有没有办法下载完整的图像?
  • @ManuelSchneid3r 你有什么办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多