简体   繁体   English

Python 上的 Telethon:如何仅下载照片?

[英]Telethon on Python: How download only photo?

stackoverflow.堆栈溢出。 I beginer python developter and need help.我是 python 开发人员,需要帮助。 I want download foto on my serwer from different telegram chenals.我想在我的服务器上从不同的电报 chenals 下载照片。 Have htis code, but it download all media including video.有 htis 代码,但它会下载所有媒体,包括视频。 How i can chenge code to download only photo?我如何才能更改代码以仅下载照片? I've read the documentation, but don't found the answer.我已经阅读了文档,但没有找到答案。 Thank you!谢谢!

from telethon.sync import TelegramClient, events
from telethon.tl.functions.messages import GetDialogsRequest
from tqdm import tqdm

with TelegramClient('name', api_id, api_hash) as client:
    result = client(GetDialogsRequest(
        offset_date=None,
        offset_id=0,
        offset_peer="username",
        limit=500,
        hash=0,
    ))

    title = 'Новинки Margaret❤'
    for chat in result.chats:
        print(chat)

        if chat.title == title:
            messages = client.get_messages(chat, limit=1000)

            for message in tqdm(messages):
                message.download_media('./' + title + '/') #I think the problem is right here.

Good Morning, I try to help you.早上好,我尽力帮助你。

I think your problem is how do you read the line:我认为您的问题是您如何阅读该行:

message.download_media('./' + title + '/')

Think that in the Telethon documentation it is shown as follows:认为在 Telethon 文档中显示如下:

client.download_media(media, path)

I have found a similar bug, try from this already solved question:我发现了一个类似的错误,尝试从这个已经解决的问题:

All credits to the user who solved it 所有功劳归于解决它的用户

Have a great day!祝你有美好的一天!

I added a file check.我添加了文件检查。 This solved my problem.这解决了我的问题。

  for message in tqdm(messages):
        if type(message.media) == telethon.MessageMediaPhoto: 
            message.download_media('./' + title + '/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM