Python 从电子邮件中解析出正文和表
我目前正在通过以下方式获取Python中电子邮件的正文/内容:Python 从电子邮件中解析出正文和表,python,html,email,parsing,Python,Html,Email,Parsing,我目前正在通过以下方式获取Python中电子邮件的正文/内容: import email message = email.message_from_file(open(file)) messages = [part.get_payload() for part in message.walk() if part.get_content_type() == 'text/plain'] 这在大多数情况下似乎都很有效,但我注意到有时有些html表没有被选中。首先是 <html> <
import email
message = email.message_from_file(open(file))
messages = [part.get_payload() for part in message.walk() if part.get_content_type() == 'text/plain']
这在大多数情况下似乎都很有效,但我注意到有时有些html表没有被选中。首先是
<html>
<style type='text/css">
如果让我猜的话,我会猜您需要添加“text/html”
但是,您应该能够通过检查该变量的内容来确定电子邮件中的内容类型
import email
message = email.message_from_file(open(file))
# Remove the content-type filter completely
messages = [(part.get_payload(), part.get_content_type()) for part in message.walk()]
# print the whole thing out so that you can see what content-types are in there.
print(message)
这将帮助您查看其中的内容类型,然后您可以筛选所需的内容类型。谢谢。它是“text/html”。我可以在元数据中找到它。