Joh*_*nak 5 python email character-encoding poplib quoted-printable
我使用 POP3 从 Gmail 帐户下载邮件并将其保存在 SQLite 数据库中以供进一步处理:
mailbox = poplib.POP3_SSL('pop.gmail.com', '995')
mailbox.user(user)
mailbox.pass_(password)
msgnum = mailbox.stat()[0]
for i in range(msgnum):
msg = '\n'.join(mailbox.retr(i+1)[1])
save_message(msg, dbmgr)
mailbox.quit()
Run Code Online (Sandbox Code Playgroud)
但是,在数据库中查找时,除了消息正文(有效负载)的最后一行之外,所有行都有尾随等号。你知道为什么会发生这种情况吗?
弗雷德里克的链接让我找到了答案。该编码称为“quoted printable”(wiki),并且可以使用quopriPython 模块(文档)对其进行解码:
msg.decode('quopri').decode('utf-8')
Run Code Online (Sandbox Code Playgroud)