python 如何提取所有的电子邮件地址在身体从eml文件

x759pob2  于 2023-10-14  发布在  Python
关注(0)|答案(1)|浏览(113)

我正在做一个项目,看起来在邮箱和提取数据的邮件。
我想提取所有的电子邮件地址在邮件正文中找到。
提取电子邮件地址:

# Python script to extract email addresses from email messages / eml
# Comment from JNevill solved the final bit
import os
import re
from email import policy
from email.parser import BytesParser

emaillocation = os.chdir(r"\\10.21.1.236\websites\vaccinatieplanner\mail\stroomz\onbezorgd")
emaillist = []

for emailmessage in os.listdir(emaillocation):
    with open(f'{emailmessage}', 'rb') as em:
        msg = BytesParser(policy=policy.default).parse(em)
    emailmsgraw = msg.get_body(preferencelist=('plain', 'html'))
    emailmsg = emailmsgraw.get_content()
    emaillist.extend(re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', emailmsg))

print(emaillist)
wfypjpf4

wfypjpf41#

在JNevill的评论的帮助下,

# Python script to extract email addresses from email messages / eml
# Comment from JNevill solved the final bit
import os
import re
from email import policy
from email.parser import BytesParser

emaillocation = os.chdir(r"\\10.21.1.236\websites\vaccinatieplanner\mail\stroomz\onbezorgd")
emaillist = []

for emailmessage in os.listdir(emaillocation):
    with open(f'{emailmessage}', 'rb') as em:
        msg = BytesParser(policy=policy.default).parse(em)
    emailmsgraw = msg.get_body(preferencelist=('plain', 'html'))
    emailmsg = emailmsgraw.get_content()
    emaillist.extend(re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', emailmsg))

print(emaillist)

相关问题