通过人们过去尝试做类似的事情。我找到的代码是
import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('---------')
pop_conn.pass_('--------')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ['\n'.join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print(message['subject'])
print(message['body'])
pop_conn.quit()
我一直收到错误:
Traceback (most recent call last):
File "E:\False Apparition\Desktop\test.py", line 10, in <module>
messages = ['\n'.join(''.join(mssg[1]) for mssg in messages)]
File "E:\False Apparition\Desktop\test.py", line 10, in <genexpr>
messages = ['\n'.join(''.join(mssg[1]) for mssg in messages)]
TypeError: sequence item 0: expected str instance, bytes found
这是因为我使用的是python 3.6而不是早期版本吗?从本质上讲,我希望能够快速解析服务器状态电子邮件以提供重要的统计信息,但只有具有特定主题行的电子邮件(因为我可以获得其他电子邮件)。我熟悉解析,但没有能够得到一个主题或身体我有点卡住。
非常感谢提前。