我有130张学生照片可供下载。我不想手动执行此操作,因此我认为Python可以提供帮助。如何将照片保存在计算机上?
我到这为止了
import sys
import pyzmail
import email
from imapclient import IMAPClient
HOST = 'imap.server.com'
USERNAME = 'meAt@foxmail.com'
PASSWORD = 'myIMAPpassword'
pathToFiles = '/home/pedro/getEmailtexts/emailTexts/'
server = IMAPClient(HOST, use_uid=True, ssl=True)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('Inbox')
messages = server.search('UNSEEN')
for uid, message_data in server.fetch(messages, 'RFC822').items():
email_message = email.message_from_bytes(message_data[b'RFC822'])
print(uid, email_message.get('From'), email_message.get('Subject'))
我得到这种输出:
54 Pedro Rodriguez studi1
55佩德罗·罗德里格斯研究2
56佩德罗·罗德里格斯(Pedro Rodriguez)studi3
57 Pedro Rodriguez再次测试
58 =?GBK?B?scu1ww ==?= 58 =?GBK?B?scu1ww ==?= =?GBK?Q?Fw:_ = B9 = F9 = D3 = EE = BC = D1_1825010321?=
59 =?GBK?B?scu1ww ==?= jpg
UID 59有一张照片,我发了,所以我尝试了这个:
len(email_message.get_payload())
2
attachment = email_message.get_payload()[1]
attachment.get_content_type()
'image/jpeg'
所以附件是我的照片。
如何将照片保存在计算机上?
我尝试过:
file = 'email59.txt'
theFile = open(pathToFiles + file, 'a')
theFile.write(attachment)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
theFile.write(attachment)
TypeError: write() argument must be str, not Message
答案 0 :(得分:-1)
您可以使用Python Imaging Library(PIL)进行编辑,然后保存照片。 以下是指向网站的链接,您可以在其中查看其安装和使用方式: https://www.geeksforgeeks.org/working-images-python/