我找不到任何可以检索 Gmail 的代码。
import poplib
from email import parser
SERVER = "pop.gmail.com"
USER = "user@gmail.com"
PASSWORD = "password"
pop_conn = poplib.POP3_SSL(SERVER)
pop_conn.user(USER)
pop_conn.pass_(PASSWORD)
#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'])
这仅产生:
TypeError Traceback (最近一次调用最后一次) <ipython-input-8-e557fa99ae8d> in () 12 个消息 = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1 )] 13 # Concat message pieces: ---> 14 messages = ["\n".join(mssg[1]) for mssg in messages] 15 #Parse message into an email object: 16 messages = [parser.Parser( ).parsestr(mssg) 用于消息中的 mssg]
<ipython-input-8-e557fa99ae8d> in (.0) 12 messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] 13 # Concat 消息碎片:---> 14 条消息 = ["\n".join(mssg[1]) for mssg in messages] 15 #将消息解析为电子邮件对象:16 条消息 = [parser.Parser().parsestr(mssg)用于消息中的消息]
类型错误:序列项 0:预期的 str 实例,找到的字节
我已经花了好几天的时间尝试进行“简单”的电子邮件检索,而我发现的每一段代码都完全不起作用。
任何人都可以真正获得带有主题等的Gmail吗?