我有这个脚本,我正在使用,我上网并修改了一下。基本上我想要的是刮取收件箱中的所有电子邮件,并将其保存为CSV格式,其中包含电子邮件,名字,姓氏格式。到目前为止,它将所有信息保存到一列CSV而不是三列..
#!/usr/bin/env python
#
# Very basic example of using Python 3 and IMAP to iterate over emails in a
# gmail folder/label. This code is released into the public domain.
#
# This script is example code from this blog post:
# http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
#
# This is an updated version of the original -- modified to work with Python 3.4.
#
import sys
import imaplib
import getpass
import email
import email.header
import datetime
import csv
EMAIL_ACCOUNT = "test@email.com"
# Use 'INBOX' to read inbox. Note that whatever folder is specified,
# after successfully running this script all emails in that folder
# will be marked as read.
EMAIL_FOLDER = "INBOX"
def process_mailbox(M):
"""
Do something with emails messages in the folder.
For the sake of this example, print some headers.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
emailList = []
out = open('email_list.csv', 'w')
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1]) # Contains all the message info including header and content
hdr = email.header.make_header(email.header.decode_header(msg['From']))
sender = str(hdr)
emailList.append(sender)
#out.write(sender)
#out.write('\n')
for c in emailList:
print (c.split(" "))
out.write(c)
out.write('\n')
out.close()
## print('Raw Date:', msg['Date'])
## # Now convert to local date-time
## date_tuple = email.utils.parsedate_tz(msg['Date'])
## if date_tuple:
## local_date = datetime.datetime.fromtimestamp(
## email.utils.mktime_tz(date_tuple))
## print ("Local Date:", \
## local_date.strftime("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, 'password')
except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
print(rv, data)
rv, mailboxes = M.list()
if rv == 'OK':
print("Mailboxes:")
print(mailboxes)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print("Processing mailbox...\n")
process_mailbox(M)
M.close()
else:
print("ERROR: Unable to open mailbox ", rv)
M.logout()
以这种格式打印出来:
['John', 'Doe', '<mail-noreply@google.com>']
['Joe', 'Doe', '<mail-noreply@google.com>']
['Jacob', 'Doe', '<mail-noreply@google.com>']
['Homer', 'Simpson-Doh!', '<mail-noreply@google.com>']
但在CSV中,它会将所有信息打印到一列中,如下所示:
Column 1
Row 1 John Doe <mail-noreply@google.com>
Row 2 Joe Doe <mail-noreply@google.com>
Row 3 Jacob Doe <mail-noreply@google.com>
但我需要3列而不是..执行out.write(c.split(“”))但这会导致错误。