Skip to content

Instantly share code, notes, and snippets.

@alaminopu
Last active December 4, 2015 17:27
Show Gist options
  • Save alaminopu/c2e5e7bf9cfaec7fcbe2 to your computer and use it in GitHub Desktop.
Save alaminopu/c2e5e7bf9cfaec7fcbe2 to your computer and use it in GitHub Desktop.
Export yahoo mail to Excel file.
#!/usr/bin/env python
import imaplib, email
import xlsxwriter
from BeautifulSoup import BeautifulSoup
# email login section
username = 'your email username'
password = 'your password'
url = "imap.mail.yahoo.com"
email_account = imaplib.IMAP4_SSL(url)
typ, accountDetails = email_account.login(username, password)
if typ != 'OK':
print 'Not able to sign in!'
else:
print 'Logged in'
# Selecting
email_account.select()
typ, data = email_account.search(None, 'ALL')
if typ != 'OK':
print 'Error Searching inbox'
else:
print 'Searching Successful'
# Excel sheet opening
workbook = xlsxwriter.Workbook('emails.xlsx')
worksheet = workbook.add_worksheet()
# writing headers of excel sheet
worksheet.write(0,0, 'SL')
worksheet.write(0,1, 'From')
worksheet.write(0,2, 'Subject')
worksheet.write(0,3, 'Message')
sl = 0
row = 1
col = 0
# email scrapping section
for num in data[0].split():
# typ, data = email_account.fetch(num, '(BODY[HEADER.FIELDS (FROM TO SUBJECT)])')
# recip,send,subj = data[0][1].split("\r\n")[:3]
# print recip
# print send
# print subj
typ, data = email_account.fetch(num, '(RFC822)')
if typ != 'OK':
print 'Error fetching mail.'
msg = email.message_from_string(data[0][1])
# wring to worksheet
sl = sl + 1
worksheet.write(row , col, str(sl))
worksheet.write(row, col+1, msg['From'])
worksheet.write(row, col+2, msg['Subject'])
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
cleantext = BeautifulSoup(part.as_string()).text
worksheet.write(row, col+3 , cleantext)
continue
if part.get('Content-Disposition') is None:
#print part.as_string()
cleantext = BeautifulSoup(part.as_string()).text
worksheet.write(row, col+3 , cleantext)
continue
row = row +1
# closing part
workbook.close()
email_account.close()
email_account.logout()
print "Done!"
@alaminopu
Copy link
Author

parsing HTML can be done with BeautifulSoup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment