Python电子邮件分析问题 [英] Python Email Parsing Issue

查看:279
本文介绍了Python电子邮件分析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在python中编写一个脚本来登录我的gmail帐户,然后很快在GUI中告诉我该消息是什么。稍后我会对代码做更多的工作,以使它稍微有点用处,但现在我只能解析我所获得的原始信息。这是我的代码:

 #阅读电子邮件脚本
导入imaplib
导入电子邮件

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username@gmail.com','passwordgoeshere')
mail.list()
mail.select(INBOX)#连接到收件箱。


result,data = mail.search(None,ALL)

ids = data [0]
id_list = ids.split )
latest_email_id = id_list [-1]

result,data = mail.fetch(latest_email_id,'(RFC822)')

raw_email = data [0] [1]

email_message = email.message_from_string(raw_email)

print(email_message ['Subject'])

现在,基本上应该尝试读出发送到我的收件箱的最新电子邮件的主题。但是,我在控制台中收到以下错误消息:

 >>> 
Traceback(最近一次调用的最后一个):
在< module>文件中,第21行的文件C:/ Users / Dhruvin Desai / Documents / Python / script.py
email_message = email.message_from_string(raw_email)
文件C:\Python33\lib\email\__init __。py,第40行,在message_from_string
返回Parser(* args ,** kws).parsestr(s)
文件C:\Python33\lib\email\parser.py,第69行,parsestr
返回self.parse(StringIO(文本),headersonly = headersonly)
TypeError:initial_value必须是str或None,而不是字节
>>>

我不知道为什么会出现,但是它告诉我<$ $的值c $ c> email_message 需要字符串格式,我试过了:

  email_message = email .message_from_string(str(raw_email))

但是运行整个脚本后,无论如何,导致控制台说



我不知道该怎么做,请帮忙。

解决方案

因为您使用的是Python3,而不是使用

  email.message_from_string(raw_email)

使用

  email.message_from_bytes(raw_email)


so I'm trying to write a script in python that logs into my gmail account and then tells me, soon in a GUI, what the message is. I will do a bit more stuff to the code later on to make it a bit program a bit more useful but right now I'm stuck on just being able to parse the raw information that I am getting. Here is my code:

#Read Email Script
import imaplib
import email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username@gmail.com', 'passwordgoeshere')
mail.list()
mail.select("INBOX") # connect to inbox.


result, data = mail.search(None, "ALL")

ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]

result, data = mail.fetch(latest_email_id, '(RFC822)')

raw_email = data[0][1]

email_message = email.message_from_string(raw_email)

print (email_message['Subject'])

Now this is basically supposed to try and read out the subject of the latest email that was delivered to my inbox. However I get the following error message in the console:

>>> 
Traceback (most recent call last):
  File "C:/Users/Dhruvin Desai/Documents/Python/script.py", line 21, in <module>
    email_message = email.message_from_string(raw_email)
  File "C:\Python33\lib\email\__init__.py", line 40, in message_from_string
    return Parser(*args, **kws).parsestr(s)
  File "C:\Python33\lib\email\parser.py", line 69, in parsestr
    return self.parse(StringIO(text), headersonly=headersonly)
TypeError: initial_value must be str or None, not bytes
>>> 

I don't know why this is coming up but since its telling me that the value of email_message needs to be in string format, I tried this:

email_message = email.message_from_string(str(raw_email))

But the result after running the entire script with that change always, no matter what, resulted in the console saying None

I don't know what to do, please help.

解决方案

Because you are using Python3, instead of using

email.message_from_string(raw_email)

Use

email.message_from_bytes(raw_email)

这篇关于Python电子邮件分析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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