从特定邮件地址逐行解析电子邮件



我正在尝试从特定邮件中提取电子邮件正文。我已经能够从该特定地址获取最新邮件。这是我试图提取的邮件-

Chat on qa.sheraspace.com
Conversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)
[06:04]     max: Name : max
Email : max@gmail.com
Phone : 01823457891
[06:04]     sammy has joined the conversation
[06:04]     sammy: hello
[06:04]     max: yes
[06:04]     sammy: what can i do for yot
No tawk.to live chat account ?  Create one for free here! re! 

但我无法完美地解析邮件。这是我的结果

b'Chat on qa.sheraspace.comrn========================================================================rnrnConversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)rnrnt[06:04]  max: Name : maxrnEmail : max@gmail.comrnPhone : 01823457891rnt[06:04]  sammy has joined the conversationrnt[06:04]  sammy: hellornt[06:04]  max: yesrnt[06:04]  sammy: what can i do for yotrnrntNo tawk live chat account ? Create one for free at http://www.tawk.torn'

.我怎样才能摆脱\r\并逐行获取该邮件。任何建议如何完美地解析它?这是我的代码

import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('yourmail', 'password')
mail.list()
mail.select('inbox')
result, data = mail.uid('search',None, '(FROM "mail from specific address")')
i = len(data[0].split())
for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = email_data[0][1]
    raw_email_string = raw_email.decode('utf-8')
    email_message = email.message_from_string(raw_email_string)
for part in email_message.walk():
    if part.get_content_type() == "text/plain":
        body = part.get_payload(decode=True)
        save_string = "F:\result\client_" + str(x) + ".txt"
        myfile = open(save_string, 'a')
        myfile.write(str(body))
        myfile.close()
    else:
        continue

您是否尝试拆分输出中得到的字符串?

C = 'Chat on qa.sheraspace.comrn===================================================== 
===================rnrnConversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)rnrnt[06:04]  max: Name : maxrnEmail : max@
 ...:gmail.comrnPhone : 01823457891rnt[06:04]  sammy has joined the conversationrnt[06:04]  sammy: hellornt[06:04]  max: yesrnt[06:04]  sammy: what can i do for yotrnrntNo tawk live chat account ? Create one for free
...:  at http://www.tawk.torn' 

然后拆分它:

C.split('rn')

给:

['Chat on qa.sheraspace.com',
'========================================================================',
'',
'Conversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)',
'',
't[06:04]  max: Name : max',
'Email : max@gmail.com',
'Phone : 01823457891',
't[06:04]  sammy has joined the conversation',
't[06:04]  sammy: hello',
't[06:04]  max: yes',
't[06:04]  sammy: what can i do for yot',
'',
'tNo tawk live chat account ? Create one for free at http://www.tawk.to',
'']

最新更新