Python:Google API - 从消息中获取 mimeTypes

Python: Google API - Getting mimeTypes from a message(Python:Google API - 从消息中获取 mimeTypes)

本文介绍了Python:Google API - 从消息中获取 mimeTypes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用 Google API 从我指定的电子邮件中获取数据.目前我可以找到消息,获取消息数据并将消息数据解码为可读格式.在此之后,我需要找到我的消息的正确部分(类型 text/html),然后使用 美丽的汤.不幸的是,我对电子邮件/Google API 的结构了解不够,无法扫描邮件的这个特定部分.

My goal is to use the Google API to take data from an email I have specified. Currently I can find the message, get the message data and decode the message data into a readable format. After this I need to find the correct part of my message (type text/html) and then scan for my link using beautiful soup. Unfortunately I don't understand enough about the structure of the email/Google API to scan for this specific part of the mail.

        try:
            message = gmail_service.users().messages().get(userId='me', id=thread['id'], format='raw').execute()
            print 'Message snippet: %s' % message['snippet']
            msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
            mime_msg = email.message_from_string(msg_str)

            print mime_msg        #this line gives the output I quoted
            for parts in mime_msg['payload']:    #this line produces error quoted
                if parts['text/html']:
                    mylink = base64.urlsafe_b64decode(part[0]['body']['data'].encode('UTF-8'))
                    print mylink

这段代码给我的错误是:

And the error that this code gives me is:

Traceback (most recent call last):
  File "gmailAPI.py", line 55, in <module>
    for parts in mime_msg['payload']:
TypeError: 'NoneType' object is not iterable

在代码的输出中,我还收到了邮件不同部分的信息,这就是我想要的部分:

In the output for the code I also receive the information on different parts of the mail, this is the part I want:

----boundary_1_81681de2-2c9a-4827-802a-91544e5e6e28
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64

PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFsLy9FTiINCiAgICJodHRwOi8vd3d3LnczLm9yZy9UUi9odG1sNC9sb29zZS5kdGQiPg0KDQo8aHRtbCBsYW5nPSJlbiI+DQo8aGVhZD4NCgk8bWV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Bsb2dvIiBzdHlsZT0iLW1zLWludGVycG9sYXRpb24tbW9kZTpiaWN1YmljOyI+PGJyLz48YnIgLz4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgIAk8L3RyPg0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIDx0cj4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPHRkIGFsaWduPSJsZWZ0IiB3aWR0aD0iMzUwIiBzdHlsZT0icGFkZGluZy1ib3R0b206IDE1cHg7IiB2YWxpZ249InRvcCIgYmdjb2xvcj0iI2ZmZmZmZiIgY2xhc3M9InN1YkxvZ28iPjxpbWcgc3JjPSJjaWQ6QlRfTGluZS5qcGciIGFsdD0ibGluZSI+PC90ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3RyPg0KCQkJCQkJPC90YWJsZT4JDQogICAgICAgICAgICAgICAgICAgICAgICA8dGFibGUgd2lkdGg9IjEwMCUiIGNlbGxwYWRkaW5nPSIwIiBjZWxsc3BhY2luZz0iMCIgYm9yZGVyPSIwIj4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA8dHI+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDx0ZCBiZ2NvbG9yPSIjZmZmZmZmIiBzdHlsZT0icGFkZGluZzogMjBweDsiIGNsYXNzPSJlbnRyeSIgdmFsaWduPSJ0b3AiPg0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8c3BhbiBzdHlsZT0iY29sb3I6IzMzMzMzMztmb250LXNpemU6MTRweDtsaW5lLWhlaWdodDoxLjI7Zm9udC1mYW1pbHk6J0hlbHZldGljYSBOZXVlJyxIZWx2ZXRpY2EsQXJpYWwsc2Fucy1zZXJpZjttYXJnaW4tYm90dG9tOjA7cGFkZGluZy10b3A6MDtwYWRkaW5nLWJvdHRvbTowO2ZvbnQtd2VpZ2h0Om5vcm1hbDsiPg0KICAgICAgICAgIC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1pbHk6J0hlbHZldGljYSBOZXVlJyxIZWx2ZXRpY2EsQXJpYWwsc2Fucy1zZXJpZjtmb250LXNpemU6MTRweDtsaW5lLWhlaWdodDoxLjI7Zm9u
dC1mYW1pbHk6J0hlbHZldGljYSBOZXVlJyxIZWx2ZXRpY2EsQXJpYWwsc2Fucy1zZXJpZjttYXJnaW4tYm90dG9tOjA7cGFkZGluZy10b3A6MDtwYWRkaW5nLWJvdHRvbTowO2ZvbnQtd2VpZ2h0Om5vcm1hbDsiPg0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPGJyLz5LaW5kIFJlZ2FyZHMsPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgVGhlIGVDb25uZWN0IE9ubGluZSBUZWFtDQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3NwYW4+PGJyIC8+PGJyIC8+DQoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDxzcGFuIHN0eWxlPSJjb2xvcjojMzMzMzMzO2ZvbnQtc2l6ZToxNHB4O2xpbmUtaGVpZ2h0OjEuMjtmb250LWZhbWlseTonSGVsdmV0aWNhIE5ldWUnLEhlbHZldGljYSxBcmlhbCxzYW5zLXNlcmlmO21hcmdpbi1ib3R0b206MDtwYWRkaW5nLXRvcDowO3BhZGRpbmctYm90dG9tOjA7Zm9udC13ZWlnaHQ6bm9ybWFsOyI+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8YnIvPkZvciBzdXBwb3J0IHBsZWFzZSBjb250YWN0OiBlbWVhLXN1cHBvcnRAYm90dG9tbGluZS5jb20gPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgVGVsOiAwODcwIDA4MSA4MjUwPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3NwYW4+DQoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3RyPg0KICAgICAgICAgICAgICAgICAgICAgICAgPC90YWJsZT4NCiAgICAgICAgICAgICAgICAgICAgICAgIDxici8+DQoJCQkJCQkJCQkNCiAgICAgICAgICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgICAgICAgICA8L3RyPg0KICAgICAgICAgICAgPC90YWJsZT4NCgkJPC90ZD4NCgk8L3RyPg0KPC90YWJsZT4NCgkJCQkNCjx0YWJsZSB3aWR0aD0iNjAwIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9IjAiIGJvcmRlcj0iMCIgYWxpZ249ImNlbnRlciIgY2xhc3M9ImZvb3RlciI+DQogICAgPHRyPg0KICAgICAgICA8dGQ+DQogICAgICAgICAgICA8dGFibGUgd2lkdGg9IjYwMCIgY2VsbHBhZGRpbmc9IjAiIGNlbGxzcGFjaW5nPSIwIiBib3JkZXI9IjAiIGFsaWduPSJjZW50ZXIiIGNsYXNzPSJ0YWJsZSIgc3R5bGU9ImJvcmRlci10b3A6MXB4IHNvbGlkICNjY2NjY2M7Ij4NCiAgICAgICAgICAgICAgICA8dHI+DQogICAgICAgICAgICAgICAgICAgIDx0ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgIDwvdGQ+DQogICAgICAgICAgICAgICAgPC90cj4NCiAgICAgICAgICAgICAgICA8dHI+DQogICAgICAgICAgICAgICAgICAgIDx0ZD48cCBzdHlsZT0iZm9udC1mYW1pbHk6dmVyZGFuYTsgY29sb3I6IzQ0NDQ0NDsgZm9udC1zaXplOjEwcHg7Ij4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICZjb3B5OyAyMDE0IEJvdHRvbWxpbmUgVGVjaG5vbG9naWVzLCBJbmMuIEFsbCBSaWdodHMgUmVzZXJ2ZWQ8L3A+PC90ZD4NCiAgICAgICAgICAgICAgICA8L3RyPgkNCiAgICAgICAgICAgIDwvdGFibGU+ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICANCiAgICAgICAgPC90ZD4NCiAgICA8L3RyPg0KPC90YWJsZT4NCgkNCjwvYm9keT4NCjwvaHRtbD4NCg==

链接到我的代码中的完整转储

我的固定代码

try:
        message = gmail_service.users().messages().get(userId='me', id=thread['id'], format='raw').execute()
        # print 'Message snippet: %s' % message['snippet']
        msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
        msg = email.message_from_string(msg_str)

        for part in msg.walk():
            msg.get_payload()
            if part.get_content_type() == 'text/html':
                mytext = base64.urlsafe_b64decode(part.get_payload().encode('UTF-8'))
                # print part.get_payload()
                print mytext

在我选择的答案文档链接上找到的信息对于解决我的问题非常宝贵!

The information found on my chosen answers documentation link was invaluable in solving my issue!

推荐答案

要在 Python 中遍历多部分消息的各个部分,您应该使用 get_payload(): https://docs.python.org/2/library/email.message.html#email.message.Message.get_payload

To iterate through the parts of a multipart message in Python, you should use get_payload(): https://docs.python.org/2/library/email.message.html#email.message.Message.get_payload

在您的示例中,对 mime_msg['payload'] 的调用正在查找名为payload"的消息标头,该标头不存在,也不是您想要的.

In your example, the call to mime_msg['payload'] is looking up a message header named "payload", which doesn't exist and is not what you want anyway.

一旦你手头有一个部件,你可以使用 part['Content-Type'] 检查它的类型来检查 Content-Type 标头.

Once you have a part in hand, you can check its type using part['Content-Type'] to examine the Content-Type header.

一般来说,MIME 消息是由部分组成的树,因此您可能需要递归.

In general, MIME messages are trees of parts, so you may need to recurse.

这篇关于Python:Google API - 从消息中获取 mimeTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Python:Google API - 从消息中获取 mimeTypes

基础教程推荐