过去半个小时我一直在挠头,但我不能纠正这个错误,让我解释一下。 基本上我想做的是做一个简单的脚本,可以下载过去6天的文字和照片,脚本在任何方面都可以正常工作,只是当我重新运行它时,它会再次下载照片,最后得到重复的图像。你知道吗
代码如下:
from datetime import timedelta
from datetime import datetime
from telethon import TelegramClient
date = datetime.now() + timedelta(-6)
api_id = 123
api_hash = 'XXX'
client = TelegramClient('SESSION', api_id, api_hash)
#Blank files have been already created
async def main():
with open("downloadedids.txt", "r+") as archivio, open("messagesaved.txt", "r+") as scaricare:
async for message in client.iter_messages('test', offset_date=date, reverse=True): ##I want to download only messages 6 days ago to now
if str(message.id) not in str(archivio.read()):
if (message.photo):
await message.download_media("Include")
scaricare.write(str(message.text))
archivio.write(str(message.id) + "\n")
with client:
client.loop.run_until_complete(main())
也许问题与异步有关?你知道吗
它再次下载它是因为你告诉它!也许你想对现有的文件做一个校验和,如果它匹配,就不保留第二个?你知道吗
相关问题 更多 >
编程相关推荐