English 中文(简体)
分析电子邮件问题
原标题:Parsing emails problems

我无法解码我正在获取的电子邮件。

脚本应该登录到电子邮件账户, 获取未读信件, 然后再保存在数据库中 。 我只需要电子邮件中的实际文本, 但是没有 html 的东西 。

I have found many examples but none of the seems to work. I have tried this and this and some more I have found.

我现在的守则是:

import imaplib, sys, email
import email.parser

myparser = email.parser.Parser()
conn = imaplib.IMAP4_SSL(host= mail.something.com )

retcode, capabilities = conn.login( username ,  XXXXX )

conn.select( Inbox , readonly = 1) # Select inbox as read-only
retcode, messages = conn.search(None,  (UNSEEN) )
if retcode ==  OK :
    for message in messages[0].split(   ):
        if message ==   :
            continue
         ret, data = conn.fetch(message, (RFC822) )
         msg = email.message_from_string(data[0][1])
#        rootMessage = myparser.parse(data[0][1])

#        print  Message %s
%s
  % (message, rootMessage)
        print msg
        print  ---------------------------------------------------------------  

 conn.close()

正如你可以看到的,这里没有解码,因为我所尝试的一切都失败了。

我对Python非常新奇,所以如果有人能引导我走上正确的方向,我会非常感激。 黑客会很好,它不是一个任务的关键脚本,但一个通用的解决方案是最好的。

- GG -G -G -G -G -G -G -G

更新:

没有错误, 问题是输出未正确解码 。

实例投入:

 This is a test message.

 Gísli

产出:

 This is a test message.

 G=EDsli
最佳回答

这有助于:

import quopri

print quopri.decodestring(msg).decode( utf8 )

或者这样:

import base64  

body = base64.b64decode(msg) 
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签