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

问题描述 投票:1回答:1

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

Chat on qa.sheraspace.com

Conversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)
[06:04]     max: Name : max
Email : [email protected]
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.com\r\n========================================================================\r\n\r\nConversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)\r\n\r\n\t[06:04]  max: Name : max\r\nEmail : [email protected]\r\nPhone : 01823457891\r\n\t[06:04]  sammy has joined the conversation\r\n\t[06:04]  sammy: hello\r\n\t[06:04]  max: yes\r\n\t[06:04]  sammy: what can i do for yot\r\n\r\n\tNo tawk live chat account ? Create one for free at http://www.tawk.to\r\n'

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

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
python string email parsing imaplib
1个回答
1
投票

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

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

然后分开它:

C.split('\r\n')

得到:

['Chat on qa.sheraspace.com',
'========================================================================',
'',
'Conversation started on : Sunday, April 07, 2019, at 06:00 (GMT+0)',
'',
'\t[06:04]  max: Name : max',
'Email : [email protected]',
'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',
'']
© www.soinside.com 2019 - 2024. All rights reserved.