【问题标题】:Getting right encoding for e-mail from gmail API从 gmail API 获取正确的电子邮件编码
【发布时间】:2019-08-14 03:07:03
【问题描述】:

我正在努力让电子邮件中的特殊字符正确显示。

我使用 Gmail API 收到这样的消息:

msg_id = '169a8fac44fd8115'
service = build('gmail', 'v1', credentials=creds)
message = service.users().messages().get(userId='me', id=msg_id).execute()
htmlpart = message['payload']['parts'][0]['parts'][1]['body']['data']

然后我尝试了以下方法:

file_data = quopri.decodestring(base64.urlsafe_b64decode(htmlpart)).decode('iso-8859-1')
file_data = base64.urlsafe_b64decode(htmlpart.encode('UTF-8')).decode('iso-8859-1')
file_data = base64.urlsafe_b64decode(htmlpart.encode('iso-8859-1')).decode('utf-8')
file_data = base64.urlsafe_b64decode(htmlpart.encode('UTF-8')).decode('utf-8')

他们都没有给我正确的输出。相反,我得到的是 €2 而不是

作为参考,该消息的标头如下:

'headers': [{'name': 'Content-Type', 'value': 'text/html; charset="UTF-8"'},
  {'name': 'Content-Transfer-Encoding', 'value': 'quoted-printable'}]

编辑:在下面添加了示例数据。我正在尝试获取电子邮件的 html,我只在下面复制其中突出显示编码问题的一部分 (You'll get)。

</tr><tr><td class="m_4364729876101169671Uber18_text_p1" align="left" style="color:rgb(0,0,0);font-family:&#39;Uber18-text-Regular&#39;,&#39;HelveticaNeue-Light&#39;,&#39;Helvetica Neue Light&#39;,Helvetica,Arial,sans-serif;font-size:16px;line-height:28px;direction:ltr;text-align:left"> Give friends free ride credit to try Uber. You&#39;ll get CN¥10 off each of your next 3 rides when they start riding. <span class="m_4364729876101169671Uber18_text_p1" style="color:#000000;font-family:&#39;Uber18-text-Regular&#39;,&#39;HelveticaNeue-Light&#39;,&#39;Helvetica Neue Light&#39;,Helvetica,Arial,sans-serif;font-size:16px;line-height:28px">Share code: 20ccv</span></td>

【问题讨论】:

    标签: python encoding gmail-api quoted-printable


    【解决方案1】:

    标题

    'headers': [{'name': 'Content-Type', 'value': 'text/html; charset="UTF-8"'},
      {'name': 'Content-Transfer-Encoding', 'value': 'quoted-printable'}]
    

    告诉您该消息由编码为 UTF-8 的文本组成,然后是带引号的可打印编码,以便它可以由仅支持 7 位字符的系统处理。

    要解码,您需要先从quoted-printable 解码,然后从UTF-8 解码结果字节。

    这样的事情应该可以工作:

    utf8 = quopri.decodestring(htmlpart)
    text = ut8.decode('utf-8')
    

    HTML 电子邮件正文可能包含character entities。这些可以使用html.unescape(在 Python 3.4+ 中可用)转换为单个字符。

    >>> import html 
    >>> h = """</tr><tr><td class="m_4364729876101169671Uber18_text_p1" align="left" style="color:rgb(0,0,0);font-family:&#39;Uber18-text-Regular&#39;,&#39;HelveticaNeue-Light&#39;,&#39;Helvetica Neue Light&#39;,Helvetica,Arial,sans-serif;font-size:16px;line-height:28px;direction:ltr;text-align:left"> Give friends free ride credit to try Uber. You&#39;ll get CN¥10 off each of your next 3 rides when they start riding. <span class="m_4364729876101169671Uber18_text_p1" style="color:#000000;font-family:&#39;Uber18-text-Regular&#39;,&#39;HelveticaNeue-Light&#39;,&#39;Helvetica Neue Light&#39;,Helvetica,Arial,sans-serif;font-size:16px;line-height:28px">Share code: 20ccv</span></td>"""
    
    
    >>> print(html.unescape(h))
    </tr><tr><td class="m_4364729876101169671Uber18_text_p1" align="left" style="color:rgb(0,0,0);font-family:'Uber18-text-Regular','HelveticaNeue-Light','Helvetica Neue Light',Helvetica,Arial,sans-serif;font-size:16px;line-height:28px;direction:ltr;text-align:left"> Give friends free ride credit to try Uber. You'll get CN¥10 off each of your next 3 rides when they start riding. <span class="m_4364729876101169671Uber18_text_p1" style="color:#000000;font-family:'Uber18-text-Regular','HelveticaNeue-Light','Helvetica Neue Light',Helvetica,Arial,sans-serif;font-size:16px;line-height:28px">Share code: 20ccv</span></td>
    

    【讨论】:

    • 谢谢。如果我使用此代码,它会输出大量看似随机的字符(我猜它是 base64)。如果我像这样添加 base64 解码: utf8 = quopri.decodestring(base64.urlsafe_b64decode(htmlpart)) file_data = utf8.decode('utf-8') 那么它告诉我 UnicodeDecodeError: 'utf-8' codec can't decode byte位置 30992 中的 0xe5:无效的继续字节
    • 您能否展示您尝试解码的数据样本?
    • 是的,添加在原帖中。
    猜你喜欢
    • 2014-09-07
    • 2018-07-28
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多