我正在遵循本指南:https : //automatetheboringstuff.com/chapter16/#calibre_link-45 来抓取电子邮件,我在使用 pyzmail.PyzMessage.factory() 时遇到问题。我不断收到 KeyError。
我从这里接受了建议:Python 电子邮件机器人 Pyzmail/IMAPclient 错误 ,但我继续收到同样的错误。
imapObj = imapclient.IMAPClient("imap.gmail.com", ssl = True)
imapObj.login("MY_EMAIL_ADDRESS", "MY_PASSWORD")
imapObj.select_folder("INBOX", readonly=False)
UIDs = imapObj.gmail_search("test1")
print(UIDs)
rawMessages = imapObj.fetch(UIDs, ["BODY[]"])
pprint.pprint(rawMessages)
message = pyzmail.PyzMessage.factory(rawMessages[40041][b'BODY[]'])
我收到此错误:
[7156]
Traceback (most recent call last):
File "C:/Users/Logan/PycharmProjects/email_sending_test/venv/main1.py", line 17, in <module>
message = pyzmail.PyzMessage.factory(rawMessages[0][b'BODY[]'])
KeyError: b'BODY[]'
defaultdict(<class 'dict'>,
{7156: {b'BODY[]': b'MIME-Version: 1.0\r\nDate: Thu, 3 Jan 2019 16:'
b'51:54 -0500\r\nMessage-ID: <CAB4Lt1swQPJvCL3ot'
b'8E7q2Pc9_C26hZxMdUgcZd9LbJUyhZbvw@mail.gmail'
b'.com>\r\nSubject: test1\r\nFrom: Rob Roberts'
b' <swimmingonanarwhal@gmail.com>\r\nTo: Rob Rob'
b'erts <swimmingonanarwhal@gmail.com>\r\nContent'
b'-Type: multipart/alternative; boundary="0000'
b'000000006f5b28057e94c5de"\r\n\r\n--000000000'
b'0006f5b28057e94c5de\r\nContent-Type: text/plai'
b'n; charset="UTF-8"\r\n\r\ntrying this ou'
b't\r\n\r\n--0000000000006f5b28057e94c5de\r\nCon'
b'tent-Type: text/html; charset="UTF-8"\r\n\r'
b'\n<div dir="ltr">trying this out</div>\r\n\r'
b'\n--0000000000006f5b28057e94c5de--',
b'SEQ': 6962}})
Process finished with exit code 1