在列表中查找内容的问题

时间:2017-05-26 11:54:00

标签: python parsing gmail

我有一个程序可以从我的电子邮件中检索邮件(通过POP),我想要检索电子邮件的主题,我使用这个:

import poplib
from email import parser
pop_srvr = poplib.POP3_SSL('pop.gmail.com')
pop_srvr.user('username')
pop_srvr.pass_('password')
messages = [pop_srvr.retr(i) for i in range(1, len(pop_srvr.list()[1]) + 1)]
messages = [b"\n".join(mssg[1]) for mssg in messages]
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
  print (message['subject'])
  print (message['body'])
pop_srvr.quit()

但是当我运行它时我得到了这个

Traceback (most recent call last):
File "C:/Users/Yousef/Desktop/parser proto.py", line 11, in <module>
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
File "C:/Users/Yousef/Desktop/parser proto.py", line 11, in <listcomp>
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\email\parser.py", line 68, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
TypeError: initial_value must be str or None, not bytes
有人告诉我,我做错了什么......

2 个答案:

答案 0 :(得分:0)

It seems linke  poplib.POP3_SSL module returns bytes object.

You probably need to change following :


    ["\n".join(mssg[1]) for mssg in messages]

 to

    [b"\n".join(mssg[1]) for mssg in messages]

我发现email模块具有message_from_bytes功能。你尝试过使用它吗?我可能会在下面的代码中被误解。请在您身边验证:

import email

messages = [pop_srvr.retr(i) for i in range(1, len(pop_srvr.list()[1]) + 1)]
messages = [b"\n".join(mssg[1]) for mssg in messages]
messages = [email.message_from_bytes(mssg) for mssg in messages]
for message in messages:
  print (message['subject'])
  print (message['body'])
pop_srvr.quit()

更多信息:parsing email contents from poplib with email module (PYTHON)

答案 1 :(得分:0)

您是否尝试将其投射到字符串?

messages = [parser.Parser().parsestr(str(mssg)) for mssg in messages]

可能会导致字符串不正确,但这样您就可以检查mssg是否确实类似于字节。如果错误消失,您可以按照适用于您的程序的方式进行投射。