【问题标题】:Python Gmail Api Base64 Decode Strange Chars In Email BodyPython Gmail Api Base64解码电子邮件正文中的奇怪字符
【发布时间】:2018-03-03 07:05:09
【问题描述】:

我正在使用 Gmail API 从我的收件箱中检索电子邮件:

query = 'to:me after:{}'.format(weekStartDate)

unreadEmailsQuery = service.users().messages().list(userId='me', q=query).execute()

# For Each Email
for message in unreadEmailsQuery['messages']:
    result = service.users().messages().get(id=message['id'],userId='me').execute()
  email_content = ''

  if 'data' in result['payload']['body'].keys():
         email_content+= result['payload']['body']['data']
    else:

        for part in result['payload']['parts']:
            email_content = part['body']['data'] + email_content



    test = bytes(str(email_content),encoding='utf-8')
    print(base64.decodebytes(test))

正确打印出简单的纯文本消息:

b'Got another one with me

但是打印出这样的html消息:

b'<body\x03B\x83B\x83B\x83B\x88\x08\x0f\x1bY]\x18H\x1a\x1d\x1d\x1c\x0bY\\]Z]\x8fH\x90\xdb\

我可以看到,直到第一个 > 从那时起字符串打印不正确,我不知道为什么。

我正在尝试从我的电子邮件中提取单词,以便我可以训练分类器,但我被卡住了。

任何帮助将不胜感激。

【问题讨论】:

    标签: python base64 gmail-api


    【解决方案1】:

    我需要使用 URl 安全 base64 解码。

    我设法通过更改最后一行来完成这项工作:

    print(base64.decodebytes(test))
    

    到:

    print(base64.urlsafe_b64decode(test))
    

    【讨论】:

    • 感谢所有支持我回答的人。如果能帮到你真的很高兴。
    猜你喜欢
    • 2018-07-28
    • 2019-07-28
    • 2017-10-28
    • 2014-09-08
    • 2011-12-26
    • 2017-03-07
    • 2015-09-25
    • 2011-02-27
    • 2012-11-06
    相关资源
    最近更新 更多