python+selenium实现BUPT疫情自动化打卡并发送邮件

"""
Date:2021.08.04
By Chris
"""
import time
from selenium import webdriver
import smtplib
from email.mime.text import MIMEText

# 经纬度设置
params = {
    "latitude": 40.164229,
    "longitude": 116.294819,
    "accuracy": 100
}

wd = webdriver.Chrome(r'C:\666\chromedriver.exe')
wd.execute_cdp_cmd("Page.setGeolocationOverride", params)
# 设置隐式等待最大时间为10s
wd.implicitly_wait(10)
wd.get('https://app.bupt.edu.cn/ncov/wap/default/index')


# 账号和密码
studentid = wd.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/input').send_keys('学号')
password = wd.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/input').send_keys('密码')
wd.find_element_by_class_name('btn').click()

time.sleep(5)

# 进入打卡界面
# 今日是否在校
wd.find_element_by_xpath('/html/body/div[1]/div/div/section/div[4]/ul/li[4]/div/div/div[1]/span[1]/i').click()
# 今日地理位置
wd.find_element_by_xpath('/html/body/div[1]/div/div/section/div[4]/ul/li[8]/div/input').click()
time.sleep(3)
# 今日是否位于中高风险地区
wd.find_element_by_xpath('/html/body/div[1]/div/div/section/div[4]/ul/li[9]/div/div/div[2]/span[1]/i').click()
# 提交打卡
# wd.find_element_by_class_name('wapcf-btn-qx').click()
wd.find_element_by_xpath('/html/body/div[1]/div/div/section/div[5]/div/a').click()
time.sleep(1)
wd.find_element_by_class_name('wapcf-btn.wapcf-btn-ok').click()



# 设置服务器所需信息
# qq邮箱服务器地址
mail_host = 'smtp.qq.com'
# qq用户名
mail_user = ''
# 密码(部分邮箱为授权码)
mail_pass = ''
# 邮件发送方邮箱地址
sender = ''
# 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
receivers = ['']

# 设置email信息
# 邮件内容设置
message = MIMEText('今日份打卡成功!!!', 'plain', 'utf-8')
# 邮件主题
message['Subject'] = '自动化打卡成功'
# 发送方信息
message['From'] = sender
# 接受方信息
message['To'] = receivers[0]

# 登录并发送邮件
try:
    smtpObj = smtplib.SMTP_SSL(mail_host)
    # 连接到服务器
    smtpObj.connect(mail_host, 465)
    # 登录到服务器
    smtpObj.login(mail_user, mail_pass)
    # 发送
    smtpObj.sendmail(sender,receivers,message.as_string())
    # 退出
    smtpObj.quit()
    print('success')
except smtplib.SMTPException as e:
    print('error', e)


wd.quit()

 

posted @ 2022-07-31 13:12  chris599  阅读(45)  评论(0编辑  收藏  举报