Python 中的邮件处理、二进制编码与 HTML 解析
1. 邮件处理
1.1 邮件解包
在 Python 中,我们可以使用以下代码将邮件消息的各个部分解包到指定目录:
import os, email
def unpack_mail(mail_file, dest_dir):
''' Given file object mail_file, open for reading, and dest_dir, a
string that is a path to an existing, writable directory,
unpack each part of the mail message from mail_file to a
file within dest_dir.
'''
with mail_file:
msg = email.message_from_file(mail_file)
for part_number, part in enumerate(msg.walk()):
if part.get_content_maintype() == 'multipart':
# we'll get each specific part later in the loop,
# so, nothing to do for the 'multipart' itself
continue
dest = par
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



