【发布时间】:2015-10-20 15:04:37
【问题描述】:
现在我来了
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('login@gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
#Get an email
result, data = mail.uid('fetch', b'1', '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_bytes(raw_email)
maintype = email_message.get_content_maintype()
#HERE COMES TROUBLES - if hmtl will be base64 string
if maintype == 'multipart':
for part in email_message.get_payload():
print(part.get_content_maintype())
if part.get_content_maintype() == 'text':
html = str(part.get_payload())
elif maintype == 'text':
html = str(email_message.get_payload())
#Now I Can parse HTML
if html is not None:
soup = BeautifulSoup(html, 'html.parser')
有些字母带有 base64 编码。怎么解码?
base64.b64encode(some_string) - 没有帮助
【问题讨论】:
-
raw_email = data[0][1].. 你快到了。yuji.wordpress.com/2011/06/22/…
-
谢谢!我是从那个教程开始的。但我想我得到字节字符串,所以它有点不同。你能告诉我最终的代码吗?
-
你能粘贴从 raw_email 输出的内容吗?将此行添加到脚本的末尾......print(raw_email)
-
我开始明白了,但是 .get_payload() 是如何工作的?
-
email.get_payload() 返回消息对象列表docs.python.org/2/library/…
标签: python email decode imaplib