使用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.htmlfrom 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类型修改为stringnames = 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.textwb.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 renum = 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 + 1elif 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 timewith 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 rechrome_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类型修改为stringnames = 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.textwb.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 + 1elif adrLines - 1 < i < adrLines + numLines:a = adr.readline().splitlines()address.append(a)i++
# 将list转为intnumber = list(map(int, number))# 报备方法while h < adrLines:wb = webdriver.Chrome(chrome_driver)if '最小的账号-1' < number[h] <= '最大的账号':student(h)h = h+1elif 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书写的不足
    在博客的书写上有许多的不规范书写,不完整书写。因为我是第一次写博客,所以有很多地方写的可能不够规范,望读者见谅。

使用Python编写程序进行疫情健康报备相关推荐

  1. 编写python程序、计算账户余额_小明有20w存款存在余额宝中,按余额宝年收益为3.35%计算,用Python编写程序计算,多少年后小明的存款达到30w?...

    [判断题]卤素灯泡是在灯泡内充入氟.氯等卤素气体. [单选题]我国刑法第12条关于溯及力的规定采取的是( ). [填空题]本地局域网 LAN 内, () 和无绳电话速率较低,主流带宽是 100kbps ...

  2. [python]编写程序产生 ISBN 号的校验位。

    @MADE BY YWL_XJTU python编写程序产生 ISBN 号的校验位. 编写程序产生 ISBN 号的校验位.任何新出版的图书都配有 ISBN 号, 2007 年以前是由 10 位数字加上 ...

  3. python编写程序的一般步骤-Python编写win程序的操作流程

    今天给大家讲的是Python怎样编写win程序的方法,对Python编写win程序的操作流程感兴趣的同学们就一起学习下具体方法吧! Python编写win程序的操作流程 1.在app.py同一目录下建 ...

  4. python编写程序-30分钟学会用Python编写简单程序

    参与文末每日话题讨论,赠送异步新书 异步图书君 学习目标 知道有序的软件开发过程的步骤. 了解遵循输入.处理.输出(IPO)模式的程序,并能够以简单的方式修改它们. 了解构成有效Python标识符和表 ...

  5. 使用Python编写程序安排期末考试监考

    问题描述:假设可监考的老师名单.考试总场次和每位老师最多监考的次数已确定,要求编写程序安排监考,并且每位老师监考的次数尽量差不多. 参考代码: 运行结果: --------董付国老师Python系列教 ...

  6. python编写程序解方程_第2章 Python初步 课后题

    [单选题]建设中国特色社会主义,把我国建设成为富强.民主.文明.和谐.美丽的社会主义现代化强国,是我国各族人民的( ) [简答题]案例系统的测试报告,提交时间为9月28日,上午九点之前 [单选题]一份 ...

  7. Python编写程序,实现对输入字符串的大小写字母翻转(即大写变小写、小写变大写)操作

    该程序通过两种方式来实现字母的翻转,一种是通过普通方式,另一种是通过自定义函数的方式来实现,该程序还对用户的输入可靠性进行判断,用户输入的必须是字母,如果输入数字,python程序会提示用户输入错误, ...

  8. python编写程序公式计算s_PYTHON程序设计实验2

    安徽工程大学 Python程序设计 实验报告 班级  物流191   姓名邹缕学号3190505117 成绩 日期     2020.3.22      指导老师修宇 实验二 顺序结构程序设计(验证性 ...

  9. python编写程序输出诗句_闲来无事能干嘛 用Python来玩诗歌接龙

    闲来无事能干嘛 用Python来玩诗歌接龙 作为一个懂Python爬虫的运维狗,闲来无事的时候总要找点乐子(睡觉不香么),哈哈,就是这么的敬业(其实是无聊).今天网盾科技给大家讲讲怎么用Python爬 ...

最新文章

  1. php删除所以文件,php如何删除所有文件
  2. 事务隔离性与隔离级别
  3. MySQL高级 之 explain执行计划详解
  4. malloc 和alloc及calloc的区别
  5. 视频驱动V4L2子系统驱动架构 - ioctl
  6. Git push “fatal: Authentication failed ”
  7. php改变图片宽高,php缩放图片(根据宽高的等比例缩放)实例介绍
  8. linux 内存管理优化,Linux性能优化实战 内存篇 阅读笔记
  9. android引入外部moudle,Android Studio3.2,调用其他Module作为依赖,出现的问题。
  10. C++ vector和stack入门习题(采用排序)
  11. token令牌防止重复提交
  12. win10-ubuntu-软件配置-开机root无密码-风扇转速调节
  13. 用友U815.0UFO报表知识点分享
  14. 威联通 Qnap PK 群晖 Synology 安全篇3
  15. 【强档推荐】动漫初音未来Ⅱ主题
  16. 机械硬盘提示:使用驱动器中的光盘之前需要将其格式化怎么办?
  17. 微软Kinect for windows SDK 使用教程一 (NUI部分)
  18. 201.微信公众号开发【文本消息】
  19. PyMC3 概率编程入门
  20. ubuntu 复制文件夹到另一目录命令

热门文章

  1. python 爬虫开发之抖音小工具
  2. 怎么查询nba2k19数据
  3. 著名油画家郑奎飞曾与三位国家最高科学技术奖获得者有过交往
  4. Python-scapy(白帽)[ARP无法欺骗手机吗][DNS欺骗不了https吗]
  5. 无网络也没关系 Google云端硬盘新增脱机模式
  6. (从零开始)基于检测前跟踪雷达目标跟踪技术的研究:第(7)周所学知识(PF-TBD感悟:纯干货,全网你绝对搜不到下面的干货)
  7. 备受瞩目 | 烤仔2019世界区块链大会行程集锦
  8. Java学习笔记:探索yzk18-commons库
  9. mapreduce 编程
  10. [学习与探索]快手视频刷积分python脚本