如何批量下载BOSS直聘候选人发往邮箱的简历附件
虽然是用在接收BOSS直聘邮件的,但也可以改为其他邮箱,不过解析需要自己修改
import imaplib
import email
import os
import base64
from email.header import decode_header
import re
# 邮箱信息
email_user = ''
email_pass = ''
# 连接到IMAP服务器
imap_server = 'imap.xxx' # 请替换成你的邮箱提供商的IMAP服务器地址
imap = imaplib.IMAP4_SSL(imap_server)
# 登录到邮箱
imap.login(email_user, email_pass)
# 选择邮箱文件夹(如收件箱)
folder = 'INBOX' # 可以根据需要更改为其他文件夹
imap.select(folder)
# 设置搜索条件(日期和发件人)
search_criteria = '(FROM "cv@service.bosszhipin.com" SINCE "14-SEP-2023")'
# 搜索匹配的邮件
status, email_ids = imap.search(None, search_criteria)
# 遍历匹配的邮件
for email_id in email_ids[0].split():
# 获取邮件
status, email_data = imap.fetch(email_id, '(RFC822)')
raw_email = email_data[0][1]
# 解析邮件
msg = email.message_from_bytes(raw_email)
# 遍历附件
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
# 获取附件名称并解码
filename = part.get_filename()
if filename:
filename, encoding = decode_header(filename)[0]
if isinstance(filename, bytes):
filename = filename.decode(encoding or 'utf-8')
# 处理Base64编码的附件内容
file_data = part.get_payload(decode=True)
file_data_decoded = base64.b64decode(file_data)
filename = filename[1:] # 保存附件
filename = "".join(x for x in filename if x.isalnum() or x in ["-", "_", "."])
# 解码 Base64 编码的附件内容
# 解码 Base64 编码的附件内容
payload = part.get_payload(decode=True)
try:
payload_decoded = payload.decode('utf-8')
except UnicodeDecodeError:
payload_decoded = payload
# 保存附件到 D 盘的 A 文件夹下
file_path = os.path.join('D:\\A\\', filename) # 替换为你想保存附件的文件夹路径
with open(file_path, 'wb') as file:
file.write(payload_decoded)
# 关闭连接
imap.logout()
本文来自博客园,作者:严_青,转载请注明原文链接:https://www.cnblogs.com/zhao-jie-li/p/17711873.html