gpt4 book ai didi

Python - 从纯文本邮件中提取正文

转载 作者:太空狗 更新时间:2023-10-29 18:34:05 26 4
gpt4 key购买 nike

我只想提取消息正文并将其返回。我可以过滤字段并显示代码段而不是正文。

def GetMimeMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_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)
return mime_msg
except errors.HttpError, error:
print 'An error occurred: %s' % error

https://developers.google.com/gmail/api/v1/reference/users/messages/get

最佳答案

谢谢。所以经过一些修改,这里的解决方案:

def GetMessageBody(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
mime_msg = email.message_from_string(msg_str)
messageMainType = mime_msg.get_content_maintype()
if messageMainType == 'multipart':
for part in mime_msg.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload()
return ""
elif messageMainType == 'text':
return mime_msg.get_payload()
except errors.HttpError, error:
print 'An error occurred: %s' % error

关于Python - 从纯文本邮件中提取正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967587/

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