使用Python进行健康报备
前言
本着自己对于Python有了一段时间的学习,正好学校要放假了,需要进行健康报备。出于自己的懒惰,便查阅有关selenium的有关资料,进行开发。
经历
一开始只是为了能够让自己偷懒来进行自动的健康报备,从开始的自己进行启动操作,到使用windows的任务计划程序来自动操作。
但是因为自己基础薄弱,在cmd的使用上也需要查阅有关的资料才能使用的得心应手。
逐步的,为了功能的日益完善,在使用上可以进行了多个人员的报备,将数据存储在txt文件中,读取txt来获取所需要使用的数据。
最后版本通过邮件的形式来反馈所操作的数据来反馈给被自动健康报备的用户。
程序是通过查阅多篇资料来进行拼拼凑凑进行完成的,对自己来说也是一种莫大的鼓励,感谢CSDN平台。
报备部分代码片
代码为寒假期间编写的代码,运行无报错后便没有去进行修改了,所以注释较少,代码较为不规范。
chromedriver 链接 chromedriver
# 我所编写的是每进行一次操作会进行一次记录
# 使用selenium进行浏览器操作
# 需要获取到需要操作的按钮、输入框等控件的name、id等
# 需要下载对应浏览器版本的driver.exe
# 我使用的是谷歌浏览器版本 87.0.4280.88(正式版本) (64 位)
# chromedriver 可下载地址: http://chromedriver.storage.googleapis.com/index.html
from selenium import webdriver
import time
import re
chrome_driver: str = r"driver\chromedriver.exe"
# 学生报备
h = 0
def student(h):
with open('record.txt', 'a+')as Wi:
print('您在为账号:', number[h], '进行报备')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime()) + '----------------------------------------------------------\n')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' 正在为账号:')
Wi.write(str(number[h]))
Wi.write('进行报备\n')
# 进入健康报备
wb.get('网址')
# 识别学号输入框
kw = wb.find_element_by_id('账号框id')
# 输入学号
kw.send_keys(number[h])
# 延时3秒
time.sleep(2)
# 点击查询
kw = wb.find_element_by_id('查询按钮id').click()
submitok = ' 已进入报备网站'
with open('record.txt', 'a+') as Wi:
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + submitok + '\n')
# 读取当前网页源码
kw = html = wb.page_source
# 获取名字
names = re.findall('姓名:<span id="Lblxm">(.*?)</span></td></tr>', html, re.S)
# 所在班级
company = re.findall('<span id="Lbldepart">(.*?)</span>', html, re.S)
# 将名字和班级的[“ ”]去掉
names = "".join(names)
company = "".join(company)
# 把list类型修改为string
names = str(names)
company = str(company)
with open('record.txt', 'a+') as Wi:
print(submitok)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " 姓名:")
Wi.write(names)
Wi.write('\n')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " 所在班级:")
Wi.write(company)
Wi.write('\n')
# 寻找第一点的否 并点击
kw = wb.find_element_by_id('/**/').click()
njk = ' 1否'
with open('record.txt', 'a+') as Wi:
print(njk)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + njk + '\n')
# 延时1秒
time.sleep(1)
# 寻找第二点的文本框 并输入值
kw = wb.find_element_by_id('txtaddress')
kw.send_keys(address[h])
with open('record.txt', 'a+')as Wi:
print('报备所在地为:', address[h])
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' 报备所在地为:')
Wi.write(str(address[h]))
Wi.write('\n')
# h = h + 1
# 寻找第三点的否 并点击
kw = wb.find_element_by_id('nhb').click()
with open('record.txt', 'a+')as Wi:
nhb = ' 3否'
print(nhb)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + nhb + '\n')
# 延时1秒
time.sleep(1)
# 寻找第四点的否 并点击
kw = wb.find_element_by_id('ncdoubt').click()
with open('record.txt', 'a+')as Wi:
ncdoubt = ' 4否'
print(ncdoubt)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ncdoubt + '\n')
# 寻找第五点的否 并点击
kw = wb.find_element_by_id('ndoubt').click()
with open('record.txt', 'a+')as Wi:
ndoubt = ' 5否'
print(ndoubt)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ndoubt + '\n')
kw = wb.find_element_by_id('njk').click()
# 延时2秒
time.sleep(2)
# 寻找报备按钮 并点击
kw = wb.find_element_by_id('submitbb').click()
with open('record.txt', 'a+')as Wi:
submitbb = ' 提交'
print(submitbb)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + submitbb + '\n')
# 读取alert信息
text = wb.switch_to.alert.text
wb.switch_to.alert.accept()
with open('record.txt', 'a+')as Wi:
print(text)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + text + '\n')
wb.close()
上面完成了学生的报备操作,可根据自己的报备网站进行更改,并非复制即可使用。
文本文件操作部分代码片
以下为文本的读取
# 以下进行了对于文本文件学号,地址,邮件查询、读取
# 以下代码为部分代码
import os
import re
num = open("IO/studentNumber.txt", 'r')
adr = open("IO/address.txt", 'r', encoding='utf-8')
eml = open("IO/emailNumber.txt", 'r')
number = []
address = []
emails = []
i = 0
#获取文件行数
with open("IO/studentNumber.txt", 'r') as numlines:
numLines = (len(numLines.readlines()))
with open("IO/address.txt", 'r', encoding='utf-8') as adrlines:
adrLines = (len(adrLines.readlines()))
with open("IO/emailNumber.txt", 'r') as emlLines:
emlLines = (len(emlLines.readlines()))
# 将记事本内容存入数组
if numlines == adrLines:
if numLines == emlLines:
while i < adrLines + numLines:
if i < adrLines:
n = num.readline().splitlines()
number.append(n)
number[i] = "".join(number[i])
e = eml.readline().splitlines()
emails.append(e)
i = i + 1
elif adrLines - 1 < i < adrLines + numLines:
a = adr.readline().splitlines()
address.append(a)
i++
# 将list转为int
number = list(map(int, number))
# 报备方法
elif numLines > emlLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比邮箱多一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比邮箱多一个\n')
elif numLines < emlLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比邮箱少一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比邮箱少一个\n')
elif numLines > adrLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比地址多一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比地址多一个\n')
elif numLines < adrLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比地址少一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比地址少一个\n')
Wi.close()
以下为写入txt文件
# 写入记事本record.txt 时间 对于开始报备进行记录
# 绝大部分的报备操作写入记事本文件已在报备方法体中进行了体现
import re
import os
import time
with open('record.txt', 'a+') as Wi:
Wi.write('写入一行\n')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '开始报备\n')
发送邮件代码片
import time
import email.message
import smtplib
#定义邮件发送内容
startTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
emailName= "姓名:"
pleaceTheUnit = "所在单位:"
reportedToTheStar = "开始报备"
userName = "正在为账号:"
toQuoteFor = "进行报备"
submitol = ' 已进入报备网站'
bbaddres = "报备所在地为:"
submitbb = " 提交"
emailAddres = "邮箱地址为:"
emailSuccessfullySent = "已经成功发送邮件"
facultyAccount = "此账号为教职工"
msg = email.message.EmailMessage()
msg["From"] = '' # 发件邮箱
msg["To"] = emails[h]
msg["Subject"] = "健康报备"
bbtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
# 发送纯文字内容
msg.set_content(
startTime + ksbb + "\r\n"
+ emailName + names + "\r\n"
+ pleaceTheUnit + company + "\r\n"
+ userName + str(number[h]) + toQuoteFor + "\r\n"
+ submitol + "\r\n"
+ njk + "\r\n"
+ bbaddres + str(address[h]) + "\r\n"
+ nhb + "\r\n"
+ ncdoubt + "\r\n"
+ ndoubt + "\r\n"
+ submitbb + "\r\n"
+ text + "\r\n"
+ emailAddres + str(emails[h]) + "\r\n"
+ emailSuccessfullySent + "\r\n"
)
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 邮件服务器,此为QQ邮箱
server.login("UserName", "Password") #邮箱账号和密码
server.send_message(msg)
server.close()
邮箱服务器可对于自己使用的邮箱进行查找端口进行修改。
综合代码片
from selenium import webdriver
import os
import email.message
import smtplib
import time
import re
chrome_driver: str = r"driver\chromedriver.exe"
num = open("IO/studentNumber.txt", 'r')
adr = open("IO/address.txt", 'r', encoding='utf-8')
eml = open("IO/emailNumber.txt", 'r')
number = []
address = []
emails = []
i = 0
h = 0
#定义邮件发送内容
startTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
emailName= "姓名:"
pleaceTheUnit = "所在单位:"
reportedToTheStar = "开始报备"
userName = "正在为账号:"
toQuoteFor = "进行报备"
submitol = ' 已进入报备网站'
bbaddres = "报备所在地为:"
submitbb = " 提交"
emailAddres = "邮箱地址为:"
emailSuccessfullySent = "已经成功发送邮件"
facultyAccount = "此账号为教职工"
# 学生报备
def student(h):
with open('record.txt', 'a+')as Wi:
print('您在为账号:', number[h], '进行报备')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime()) + '----------------------------------------------------------\n')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' 正在为账号:')
Wi.write(str(number[h]))
Wi.write('进行报备\n')
# 进入健康报备
wb.get('网址')
# 识别学号输入框
kw = wb.find_element_by_id('账号框id')
# 输入学号
kw.send_keys(number[h])
# 延时3秒
time.sleep(2)
# 点击查询
kw = wb.find_element_by_id('查询按钮id').click()
submitok = ' 已进入报备网站'
with open('record.txt', 'a+') as Wi:
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + submitok + '\n')
# 读取当前网页源码
kw = html = wb.page_source
# 获取名字
names = re.findall('姓名:<span id="Lblxm">(.*?)</span></td></tr>', html, re.S)
# 所在班级
company = re.findall('<span id="Lbldepart">(.*?)</span>', html, re.S)
# 将名字和班级的[“ ”]去掉
names = "".join(names)
company = "".join(company)
# 把list类型修改为string
names = str(names)
company = str(company)
with open('record.txt', 'a+') as Wi:
print(submitok)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " 姓名:")
Wi.write(names)
Wi.write('\n')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + " 所在班级:")
Wi.write(company)
Wi.write('\n')
# 寻找第一点的否 并点击
kw = wb.find_element_by_id('/**/').click()
njk = ' 1否'
with open('record.txt', 'a+') as Wi:
print(njk)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + njk + '\n')
# 延时1秒
time.sleep(1)
# 寻找第二点的文本框 并输入值
kw = wb.find_element_by_id('txtaddress')
kw.send_keys(address[h])
with open('record.txt', 'a+')as Wi:
print('报备所在地为:', address[h])
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ' 报备所在地为:')
Wi.write(str(address[h]))
Wi.write('\n')
# h = h + 1
# 寻找第三点的否 并点击
kw = wb.find_element_by_id('nhb').click()
with open('record.txt', 'a+')as Wi:
nhb = ' 3否'
print(nhb)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + nhb + '\n')
# 延时1秒
time.sleep(1)
# 寻找第四点的否 并点击
kw = wb.find_element_by_id('ncdoubt').click()
with open('record.txt', 'a+')as Wi:
ncdoubt = ' 4否'
print(ncdoubt)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ncdoubt + '\n')
# 寻找第五点的否 并点击
kw = wb.find_element_by_id('ndoubt').click()
with open('record.txt', 'a+')as Wi:
ndoubt = ' 5否'
print(ndoubt)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ndoubt + '\n')
kw = wb.find_element_by_id('njk').click()
# 延时2秒
time.sleep(2)
# 寻找报备按钮 并点击
kw = wb.find_element_by_id('submitbb').click()
with open('record.txt', 'a+')as Wi:
submitbb = ' 提交'
print(submitbb)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + submitbb + '\n')
# 读取alert信息
text = wb.switch_to.alert.text
wb.switch_to.alert.accept()
with open('record.txt', 'a+')as Wi:
print(text)
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + text + '\n')
msg = email.message.EmailMessage()
msg["From"] = '' # 发件邮箱
msg["To"] = emails[h]
msg["Subject"] = "健康报备"
bbtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
# 发送纯文字内容
msg.set_content(
startTime + ksbb + "\r\n"
+ emailName + names + "\r\n"
+ pleaceTheUnit + company + "\r\n"
+ userName + str(number[h]) + toQuoteFor + "\r\n"
+ submitol + "\r\n"
+ njk + "\r\n"
+ bbaddres + str(address[h]) + "\r\n"
+ nhb + "\r\n"
+ ncdoubt + "\r\n"
+ ndoubt + "\r\n"
+ submitbb + "\r\n"
+ text + "\r\n"
+ emailAddres + str(emails[h]) + "\r\n"
+ emailSuccessfullySent + "\r\n"
)
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 邮件服务器,此为QQ邮箱
server.login("UserName", "Password") #邮箱账号和密码
server.send_message(msg)
server.close()
wb.close()
#获取文件行数
with open("IO/studentNumber.txt", 'r') as numlines:
numLines = (len(numLines.readlines()))
with open("IO/address.txt", 'r', encoding='utf-8') as adrlines:
adrLines = (len(adrLines.readlines()))
with open("IO/emailNumber.txt", 'r') as emlLines:
emlLines = (len(emlLines.readlines()))
# 将记事本内容存入数组
if numlines == adrLines:
if numLines == emlLines:
while i < adrLines + numLines:
if i < adrLines:
n = num.readline().splitlines()
number.append(n)
number[i] = "".join(number[i])
e = eml.readline().splitlines()
emails.append(e)
i = i + 1
elif adrLines - 1 < i < adrLines + numLines:
a = adr.readline().splitlines()
address.append(a)
i++
# 将list转为int
number = list(map(int, number))
# 报备方法
while h < adrLines:
wb = webdriver.Chrome(chrome_driver)
if '最小的账号-1' < number[h] <= '最大的账号':
student(h)
h = h+1
elif numLines > emlLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比邮箱多一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比邮箱多一个\n')
elif numLines < emlLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比邮箱少一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比邮箱少一个\n')
elif numLines > adrLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比地址多一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比地址多一个\n')
elif numLines < adrLines:
with open('record.txt', 'a+')as Wi:
print('您的学号比地址少一个')
Wi.write(time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) + '您的学号比地址少一个\n')
Wi.close()
# 操作结束,程序退出
邮件效果图
- 隐私部分已进行打码操作
对于程序的自启动操作
- 程序的自启动将Python未封装成exe的话是需要调用到python.exe来操作.py文件的。
- 如果已经对于封装好了的文件则可以直接启动exe启动即可自动操作。
后续升级方向
- 将发送邮件操作转换为发送QQ群信息操作
- 在反馈的信息中添加报备所在地的天气信息一并反馈
- 将报备的信息可进行发送QQ信息修改
结语
- 程序的制作有许多的地方都不规范,例如命名。
在程序的制作的时候,为了图自己的一时快捷便利的操作,常常以拼音的首字母进行命名,在对于程序源码的整理时,往往需要费用大量的时间去对不规范命名进行解读,不仅会浪费自己的时间,也会对自己对于后续的优化、维护带来麻烦。 - 程序的制作自己思考比获得成品更有意义
自己所制作的程序,享受的并非单单只是程序制作完成后给自己带来的便利,虽然这也是一部分,但是过程中的学习,更加是有价值的地方,让自己的不断获得的经验,不断的向前进发。 - 程序中的不足
在整理的过程中,有许多地方需要更名修改、删除、添加等,所以有些许地方的代码片可能会出现缺失或者报错的情况,此类情况我一时间也无法去进行解决,还望见谅。 - CSDN书写的不足
在博客的书写上有许多的不规范书写,不完整书写。因为我是第一次写博客,所以有很多地方写的可能不够规范,望读者见谅。