为什么“=0D\r\n`出现在使用Python的电子邮件抓取中

2024-04-24 16:12:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用imaplib从Gmail中检索电子邮件,并将其输入Beautifulsoup来解析电子邮件的HTML代码。在

M = imaplib.IMAP4_SSL('imap.gmail.com')
M.select('Inbox')
rv, data = M.SEARCH(None, '(FROM "foo@bar.com")')
if rv == 'OK':
    for num in data[0].split():
        typ, data = M.fetch(num, '(RFC822)')
        html = data[0][1].strip()

问题:为什么html包含这么多=0D\r\n的出现?它们是什么?我们应该完全删除它们还是用另一个字符串替换它们?在

示例:

^{pr2}$

更新

尝试分析为quoted-printible,使用

html = html.decode('utf-8')
html = email.message_from_string(html)

它清理了html好多了,但是在几乎每一行的末尾仍然有{}的痕迹。我们怎样才能解决这个问题?在

还有字符串3D出现在=之后。。。在

</tr>=0D
<tr>=0D
    <td height=3D"12"></td>=0D
</tr>=0D
<tr></tr>=0D
<tr>=0D
    <td class=3D"height35" height=3D"50"></td>=0D
</tr>=0D
</tbody>=0D

Tags: 字符串代码comdata电子邮件htmlnumtr