Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从电子邮件中解析出正文和表_Python_Html_Email_Parsing - Fatal编程技术网

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> <

我目前正在通过以下方式获取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>
<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”。我可以在元数据中找到它。