使用python将gmail文本内容导入到文本文件不会返回任何内容 [英] importing gmail text content to a text file with python returns nothing

查看:164
本文介绍了使用python将gmail文本内容导入到文本文件不会返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,将我的Gmail邮件帐户中的所有短信/内容(不是附件)都发送到一个文本文件,并且我找到了一段可以做到的承诺。我有Python 3.4和2.7都安装在赢7。我知道的PHP一点,但蟒蛇我是零。现在,我将代码复制到记事本文本中,并将其保存为 test.py

 导入imaplib 
导入电子邮件
mail = imaplib.IMAP4_SSL('imap .gmail.com')
mail.login('yeyemailid@gmail.com','mypassword')
mail.list()
mail.select('inbox')$ b $ (i)中b的b
结果,data = mail.uid('search',None,ALL)
i = len(data [0] .split())
。 :
latest_email_uid = data [0] .split()[x]
结果,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)
用于email_message.walk()中的部分:
if part.get_content_type()==text / plain:
body = part.get_payload(decode = True)
save_string = rF:\result\email_+ str(x)+.txt
myfile = open(save_string,'a')
myfile.write(body.decode('utf-8'))
m yfile.close()
else:
continue

问题:运行时的代码没有任何回报。

更新:其实我在这里经历了很多线程。最接近我问的是这里此处,但没有人有统一的答案和解决方案散布在一些零碎中,或者它太具体,所以我很难从中理解它。相信我,我正在测试和尝试。即使是类似的问题在这里仍然没有得到答案。

解决方案

对于那些可能觉得有用的人。这个脚本在Python 3.4上可以正常运行。

save_string =F:\\result\\email_+ str(x)+.txt



myfile.write(str(body))



只需根据需要更改目录和文件夹名称即可。感谢原始的代码提供者


I was looking for a solution to get all my text messages/content (not attachments) in my gmail's email-account to a text file and I found a piece of code which promises to do so. I have python 3.4 and 2.7 both installed on win 7. I know php a bit but python I am zero. Right now, I have the code copied into a notepad text and saved it as test.py. Here is the complete code.

import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myemailid@gmail.com', 'mypassword')
mail.list() 
mail.select('inbox')

result, data = mail.uid('search', None, "ALL")
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 = r"F:\result\email_" + str(x) + ".txt"
        myfile = open(save_string, 'a')
        myfile.write(body.decode('utf-8'))
        myfile.close()
    else:
        continue

ISSUE : The code when run gives nothing in return.

UPDATE : Actually I have been going through a lot of threads here. Closest to what I am asking are here and here but no one has a unified answer and the solutions are scattered in bits and pieces, or it is too specific so it is very difficult for me to make sense out of it. Trust me, I am testing and trying. Even a Similar question remains unanswered here.

解决方案

For those who may find it useful. This script will run fine on Python 3.4 with these changes.

save_string = "F:\\result\\email_" + str(x) + ".txt"

myfile.write(str(body)).

Just change the directory and the folder name according to your need. Thanks to the original code provider.

这篇关于使用python将gmail文本内容导入到文本文件不会返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆