0

我有一个收件箱,我需要编写一个代码,每次有新消息到达时触发,并提取该电子邮件的正文。

到目前为止我有这个代码:

import poplib, email
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
email_user = 'email@gmail.com'
email_pass = 'password'

pop_conn.user(email_user)
pop_conn.pass_(email_pass)

#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, 
len(pop_conn.list()[1]) + 1)]

#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message.get_payload(None, True))

pop_conn.quit()

我得到的错误是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2a08d6a8ccf9> in <module>()
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

<ipython-input-6-2a08d6a8ccf9> in <listcomp>(.0)
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

/Applications/anaconda3/lib/python3.6/email/parser.py in parsestr(self, text, headersonly)
 66         the file.
 67         """
---> 68         return self.parse(StringIO(text), headersonly=headersonly)
 69 
 70 

TypeError: initial_value must be str or None, not tuple

TypeError: initial_value must be str or None, not tuple

为什么我得到一个元组?如何从消息[n] 中提取正文?

4

1 回答 1

0

from 的返回值retr()是一个元组。元组中的第二个值是包含实际消息的行列表。有关详细信息,请参阅 Pythonpoplib文档

# Get messages from server
popped = ['\n'.join(pop_conn.retr(i)[1])
    for i in range(1, len(pop_conn.list()[1]) + 1)]

# Parse message into an email object
messages = [parser.Parser().parsestr(mssg) for mssg in popped]

还要注意我们如何避免为不同类型的对象重用相同的变量名。

于 2018-11-11T19:05:13.120 回答