gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:36:55 25 4
gpt4 key购买 nike

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

        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

这段代码给我的错误是:

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

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

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

PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFsLy9FTiINCiAgICJodHRwOi8vd3d3LnczLm9yZy9UUi9odG1sNC9sb29zZS5kdGQiPg0KDQo8aHRtbCBsYW5nPSJlbiI+DQo8aGVhZD4NCgk8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD11dGYtOCI+DQoJPHRpdGxlPlNpZ251cDwvdGl0bGU+DQo8L2hlYWQ+DQoNCjxib2R5IGJnY29sb3I9IiNmZmZmZmYiIHRvcG1hcmdpbj0iMCIgbGVmdG1hcmdpbj0iMCIgbWFyZ2luaGVpZ2h0PSIwIiBtYXJnaW53aWR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O2ZvbnQtd2VpZ2h0Om5vcm1hbDsiPg0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPGJyLz5LaW5kIFJlZ2FyZHMsPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgVGhlIGVDb25uZWN0IE9ubGluZSBUZWFtDQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3NwYW4+PGJyIC8+PGJyIC8+DQoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDxzcGFuIHN0eWxlPSJjb2xvcjojMzMzMzMzO2ZvbnQtc2l6ZToxNHB4O2xpbmUtaGVpZ2h0OjEuMjtmb250LWZhbWlseTonSGVsdmV0aWNhIE5ldWUnLEhlbHZldGljYSxBcmlhbCxzYW5zLXNlcmlmO21hcmdpbi1ib3R0b206MDtwYWRkaW5nLXRvcDowO3BhZGRpbmctYm90dG9tOjA7Zm9udC13ZWlnaHQ6bm9ybWFsOyI+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8YnIvPkZvciBzdXBwb3J0IHBsZWFzZSBjb250YWN0OiBlbWVhLXN1cHBvcnRAYm90dG9tbGluZS5jb20gPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgVGVsOiAwODcwIDA4MSA4MjUwPGJyIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3NwYW4+DQoNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICA8L3RyPg0KICAgICAgICAgICAgICAgICAgICAgICAgPC90YWJsZT4NCiAgICAgICAgICAgICAgICAgICAgICAgIDxici8+DQoJCQkJCQkJCQkNCiAgICAgICAgICAgICAgICAgICAgPC90ZD4NCiAgICAgICAgICAgICAgICA8L3RyPg0KICAgICAgICAgICAgPC90YWJsZT4NCgkJPC90ZD4NCgk8L3RyPg0KPC90YWJsZT4NCgkJCQkNCjx0YWJsZSB3aWR0aD0iNjAwIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9IjAiIGJvcmRlcj0iMCIgYWxpZ249ImNlbnRlciIgY2xhc3M9ImZvb3RlciI+DQogICAgPHRyPg0KICAgICAgICA8dGQ+DQogICAgICAgICAgICA8dGFibGUgd2lkdGg9IjYwMCIgY2VsbHBhZGRpbmc9IjAiIGNlbGxzcGFjaW5nPSIwIiBib3JkZXI9IjAiIGFsaWduPSJjZW50ZXIiIGNsYXNzPSJ0YWJsZSIgc3R5bGU9ImJvcmRlci10b3A6MXB4IHNvbGlkICNjY2NjY2M7Ij4NCiAgICAgICAgICAgICAgICA8dHI+DQogICAgICAgICAgICAgICAgICAgIDx0ZD4NCiAgICAgICAgICAgICAgICAgICAgICAgIDwvdGQ+DQogICAgICAgICAgICAgICAgPC90cj4NCiAgICAgICAgICAgICAgICA8dHI+DQogICAgICAgICAgICAgICAgICAgIDx0ZD48cCBzdHlsZT0iZm9udC1mYW1pbHk6dmVyZGFuYTsgY29sb3I6IzQ0NDQ0NDsgZm9udC1zaXplOjEwcHg7Ij4NCiAgICAgICAgICAgICAgICAgICAgICAgICAgICZjb3B5OyAyMDE0IEJvdHRvbWxpbmUgVGVjaG5vbG9naWVzLCBJbmMuIEFsbCBSaWdodHMgUmVzZXJ2ZWQ8L3A+PC90ZD4NCiAgICAgICAgICAgICAgICA8L3RyPgkNCiAgICAgICAgICAgIDwvdGFibGU+ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICANCiAgICAgICAgPC90ZD4NCiAgICA8L3RyPg0KPC90YWJsZT4NCgkNCjwvYm9keT4NCjwvaHRtbD4NCg==

Link to full dump from my code

编辑:我的固定代码

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

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

最佳答案

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

在您的示例中,对 mime_msg['payload'] 的调用正在查找名为“payload”的消息 header ,它不存在,也不是您想要的。

一旦您掌握了零件,就可以使用 part['Content-Type'] 检查其类型以检查 Content-Type header 。

一般来说,MIME 消息是部件树,因此您可能需要递归。

关于Python:Google API - 从消息中获取 mimeTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com