import re
import poplib
# 引入用来解析邮件相关信息的模块
import time
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
# 引入相关时间库
from datetime import datetime
from selenium import webdriver
# 输入自己163的邮箱地址。
user_email_address = ''
# 邮箱的授权码,注意:不是登录密码
user_password = ''
# 这个是163邮箱的pop3的服务器地址,各个邮箱平台的POP3的服务器地址都是不同的
# 例如:qq邮箱的pop3服务器地址是:pop.qq.com
pop_server_host = 'pop3.mxhichina.com'
# 邮箱对应的pop服务器的监听端口
# (如果设置POP3的SSL加密方式连接的话,则端口为:995),否则就是端口为110
pop_server_port = 995
driver = webdriver.Chrome()
driver.maximize_window()
def connect_email_by_pop3(number,key_words):
try:
# 连接pop服务器。如果没有使用SSL,将POP3_SSL()改成POP3(),且监听端口改为:110即可
email_server = poplib.POP3_SSL(host=pop_server_host, port=pop_server_port, timeout=10)
print("连接pop服务器-------正常,开始验证用户邮箱")
except:
print("连接pop服务器-------异常,退出")
exit(1)
try:
# 验证用户邮箱
email_server.user(user_email_address)
print("用户邮箱验证-------正常,开始验证邮箱授权码")
except:
print("用户邮箱验证-------异常,退出")
exit(1)
try:
# 验证邮箱密码是否正确,注意不是登录密码,是授权码
email_server.pass_(user_password)
print("邮箱授权码验证-------正常,开始接受邮箱以及附件")
except:
print("邮箱授权码验证-------异常,退出")
exit(1)
# 开始处理邮箱相关信息
parse_email_server(email_server,number,key_words)
def parse_email_server(email_server,number,key_words):
_, mails, _ = email_server.list()
# num, total_size = email_server.stat()
# mails存储了邮件编号列表,
index = len(mails)
# 倒序遍历邮件
for i in range(index, index-number, -1):
# 倒序遍历邮件,这样取到的第一封就是最新邮件
_, lines, _ = email_server.retr(i)
# lines存储了邮件的原始文本的每一行,
# 邮件的原始文本:# lines是邮件内容,列表形式使用join拼成一个byte变量
msg_content = b'\r\n'.join(lines).decode('utf-8')
# 解析邮件:
msg = Parser().parsestr(msg_content)
# 解析邮件具体内容,包括正文,标题,和附件
parser_content(msg, 0,key_words,driver)
# 退出
email_server.quit()
def parser_content(msg, indent,key_words,driver):
is_chendu = False
if indent == 0:
# 邮件的From, To, Subject存在于根对象上:
# 调用解析邮件头部内容的函数
is_chendu,head = parser_email_header(msg,key_words)
if not is_chendu:
return
if (msg.is_multipart()):
# 如果邮件对象是一个MIMEMultipart,
# get_payload()返回list,包含所有的子对象:
parts = msg.get_payload()
for n, part in enumerate(parts):
# 递归打印每一个子对象:
return parser_content(part, indent + 1,key_words,driver)
else:
# 解析正文
content_type = msg.get_content_type()
if content_type == 'text/plain' or content_type == 'text/html':
# 纯文本或HTML内容:
content = msg.get_payload(decode=True)
# 要检测文本编码:
charset = guess_charset(msg)
if charset:
content = content.decode(charset)
if "2023年毕业" in content:
ms = parse_content(content)
print(head,ms)
if driver.current_url != 'data:,':
windows = driver.window_handles
driver.execute_script("window.open('','_blank');")
driver.switch_to.window(windows[-1])
driver.get(ms)
# print('%s正文内容为: %s' % (' ' * indent, content))
return 1
def open_chrome(url):
pass
def parse_content(content):
pattern = r'<tr><td style="font-weight:600;font-size: 20px;color: #00a9e0;">([^><]+)</td></tr>'
info = re.findall(pattern, content)
# print(info)
rpattern = r'<a href="(https://hr.shixiseng.com/#/email/view/.+)" style="display:block;width:120px;text-align:center;padding: 10px 0;vertical-align:middle; color: #ffffff;background-color: #00a9e0;text-decoration: none;">查看完整简历</a></td></tr>'
res = re.findall(rpattern, content)
# print(res)
# print(info[0],res[0])
return res[0]
# 解析邮件
def parser_email_header(msg,keywords):
# 解析邮件标题
subject = msg['Subject']
value, charset = decode_header(subject)[0]
if charset:
value = value.decode(charset)
info = filter(lambda x:x in value,key_words)
return 1 if list(info) else 0,value
# 解码
def decode_str(s):
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
# 猜测字符编码
def guess_charset(msg):
# 先从msg对象获取编码:
charset = msg.get_charset()
if charset is None:
# 如果获取不到,再从Content-Type字段获取:
content_type = msg.get('Content-Type', '').lower()
for item in content_type.split(';'):
item = item.strip()
if item.startswith('charset'):
charset = item.split('=')[1]
break
return charset
# 邮件时间处理函数
def parse_mail_time(mail_datetime):
GMT_FORMAT = "%a, %d %b %Y %H:%M:%S"
GMT_FORMAT2 = "%d %b %Y %H:%M:%S"
index = mail_datetime.find(' +0')
if index > 0:
mail_datetime = mail_datetime[:index] # 去掉+0800
formats = [GMT_FORMAT, GMT_FORMAT2]
for ft in formats:
try:
mail_datetime = datetime.strptime(mail_datetime, ft)
return mail_datetime
except:
pass
raise Exception("邮件时间格式解析错误")
# 比较规范写法,象征着程序入口
if __name__ == "__main__":
number = 50
key_words = ["成都","四川","西南"]
connect_email_by_pop3(number,key_words)
# parse_content(html)