python - 从 mbox 写入 html 文件
问题描述
在 Yahoo 群组关闭之前,您可以将群组的内容下载到 mbox 文件中。我正在尝试将 mbox 文件转换为一系列 html 文件 - 每条消息一个。我的问题是处理 html 中的编码和特殊字符。这是我的尝试:
import mailbox
the_dir = "/path/to/file"
mbox = mailbox.mbox(the_dir + "12394334.mbox")
html_header = """<!DOCTYPE html>
<html>
<head>
<title>Email message</title>
</head>
<body>"""
html_footer = '</body></html>'
for message in mbox:
mess_from = message['from']
subject = message['subject']
time_received = message['date']
if message.is_multipart():
content = ''.join(str(part.get_payload(decode=True)) for part in message.get_payload())
else:
content = message.get_payload(decode=True)
content = str(content)[2:].replace('\\n', '<br/>')
subject.replace('/', '-')
fname = subject + " " + time_received + '.html'
with open(the_dir + 'html/' + fname , 'w') as the_file:
the_file.write(html_header)
the_file.write('<br/>' + 'From: ' + mess_from)
the_file.write('<br/>' + 'Subject: ' + subject)
the_file.write('<br/>' + 'Received: ' + time_received + '<br/><br/>')
the_file.write(content)
消息的内容在撇号和其他特殊字符之前有反斜杠,如下所示:
星级,目前适用于 \xa311.99 [理想的圣诞礼物]。广告结束-Seroiusly,如果您在小船上没有一本像样的书
我的问题是,获取电子邮件内容并将其写入具有正确字符的 html 文件的最佳方法是什么。我不能成为第一个遇到这个问题的人。
解决方案
我找到了这个问题的答案。
首先,我需要通过子类型 (part.get_content_subtype()) 来识别 html。这就是我知道我有一个 html 子类型的方式。
然后我需要使用 part.get_charsets() 获取字符集。有一个 part.get_charset() 但它总是返回 None 所以我取 get_charsets() 的第一个元素
get_payload 似乎与 decode=True 参数一致,这意味着它不会解码有效负载。然后我使用我之前得到的字符集解码消息。否则,我使用 decode=False 对其进行解码。
如果是文本,我会去掉换行符等并添加一个 html 标题,然后写入文件。
接下来的工作,
- 使用 BeautifulSoup 将发件人信息/主题添加到
- 弄清楚如何处理附件并将 html 文件链接到它们
- 有些字符仍然没有显示,如“£”等。
文本
import mailbox
the_dir = "/path/to/mbox/"
mbox = mailbox.mbox(the_dir + "12394334.mbox")
html_footer = "</body></html>"
html_flag = False
for message in mbox:
mess_from = message['from']
subject = message['subject']
time_received = message['date']
fname = subject + " " + time_received
fname = fname.replace('/', '-')
if message.is_multipart():
contents_text = []
contents_html = []
for part in message.walk():
maintype = part.get_content_maintype()
subtype = part.get_content_subtype()
if maintype == 'multipart' or maintype == 'message':
# Reject containers
continue
if subtype == 'html':
enc = part.get_charsets()
if enc[0] is not None:
contents_html.append(part.get_payload(decode=True).decode(enc[0]))
else:
contents_html.append(part.get_payload(decode=False))
elif subtype == 'text':
contents_text.append(part.get_payload(decode=False))
else: #I will use this to process attachmnents in the future
continue
if len(contents_html)> 0:
if len(contents_html)>1:
print('multiple html') #This hasn't happened yet
html_flag = True
content = '\n\n'.join(contents_html)
else:
html_flag = False
else:
content = message.get_payload(decode=False)
content = content.replace('\\n', '<br/>')
content = content.replace('=\n', '<br/>')
content = content.replace('\n', '<br/>')
content = content.replace('=20', '')
html_header = f""" <!DOCTYPE html>
<html>
<head>
<title>{fname}</title>
</head>
<body>"""
content = (html_header + '<br/>' +
'From: ' + mess_from + '<br/>'
+ 'Subject: ' + subject + '<br/>' +
'Received: ' + time_received + '<br/><br/>' +
content + html_footer)
with open(the_dir + "html/" + fname + ".html", "w") as the_file:
the_file.write(content)
打印('完成!')
推荐阅读
- javascript - 如何中止/取消由 Javascript 代码引起的 HTTP 请求?
- jquery-ui - 在 jQuery UI 日期选择器中一周只显示两天
- python - 按另一列分组的二进制目标变量的 value_counts 比率
- python - 如何在 Django 中使用 CSV 文件生成图形?
- node.js - 无法使用 node/express 对工作文件上传进行单元测试
- windows - 更新 desktop.ini 和 .ico 文件
- kubernetes - Kubernetes:我们如何列出特定命名空间中 N 天内修改的所有对象?
- c++ - 当每个应该使用 std::stof 和 atof 有什么区别?
- android - 我无法确定是什么导致我的应用出现此意外上传错误
- c# - 如何解决 Asp.Net MVC 中与 ActionName 相关的问题?