gpt4 book ai didi

python - 如何将媒体下载到 Telethon 上的特定路径

转载 作者:行者123 更新时间:2023-12-04 16:25:14 24 4
gpt4 key购买 nike

我正在研究用于从 Telegram 下载媒体的 Telethon download_media 和 _download_document 方法。我的代码是这样的:

from telethon import TelegramClient

api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)

async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)

# You can download media from messages, too!
# The method will return the path where the file was saved.
if message.photo:
path = await message.download_media()
print('File saved to', path) # printed after download is done

with client:
client.loop.run_until_complete(main())

但此代码无法将媒体下载到特定路径,以及如何获取已保存文件的名称

最佳答案

Docs of telethon表明 download_media 方法接受名为 file 的参数,即

The output file path, directory, or stream-like object. If the pathexists and is a file, it will be overwritten. If file is the typebytes, it will be downloaded in-memory as a bytestring (e.g.file=bytes).

我没有能力测试它,但是像替换

message.download_media()

message.download_media(file="path/to/downloads_dir")

应该可以。

关于python - 如何将媒体下载到 Telethon 上的特定路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66100409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com