疫情当下,我们老师让我们一天提交两次温度信息,上下午各一次。信息包括姓名,学号,温度,日期,居住地,同居人情况等等。
对于及其懒惰的我来说,每次都得填这些信息太麻烦了。所以就写了一个脚本,来帮助我完成,每次只需要改动需要修改的信息就行了,方便。
代码如下:
from splinter.browser import Browser
import time
import win32api, win32con
# 驱动
driver_name = 'chrome'
"""已修改为自动获取,无需改动"""
# 运行的时候请自行改为对应的相对路径或者绝对路径
executable_path = 'D:/Workspace/Pycharm/designer/Temperature/chromedriver.exe' # 驱动所在的路径
"""已修改为自动获取,无需改动"""
# 运行的时候请自行改为对应的相对路径或者绝对路径
# CONFIG_PATH = "D:\\Workspace\\Pycharm\\designer\\Temperature\\conf.ini" # 配置文件所在的路径
class Temperature(object):
def __init__(self):
# 读取配置信息
config = Temperature.readconfig()
# 前边带_的,其值是基本不改变的
self._class = config['class'] # 班级
self._name = config['name'] # 学生姓名
self._id = config['id'] # 学号
self._address = config['address'] # 居住地
self._date = "" # 日期,已设置为系统自动获取
self._am_or_pm = "" # 上午还是下午,已设置为系统自动获取
self.temp = config['temp'] # 体温
self._phone = config['phone'] # 家庭联系电话(同住家人)
self.condition = config['condition'] # 同住人身体状况
self.activitie = config['activitie'] # 当天活动去向
"""网址"""
self.url = "http://huahang2020jike.mikecrm.com/I0Jtvc2"
# 获取日期
self._setDate()
# 加载驱动
self.driver = Browser(driver_name=driver_name, executable_path=executable_path)
def start(self):
# 如果想要使用浏览器默认的大小,请注释掉下边的两行
widthResolution, heightResolution = Temperature.getResolution() # 获取屏幕的分辨率
self.driver.driver.set_window_size(widthResolution, heightResolution) # 最大化浏览器
# sleep(1)
# with Browser(driver_name=driver_name, executable_path=executable_path) as driver:
self.driver.visit(self.url)
try:
print("正在提交信息,请稍后...")
time.sleep(1)
# 提交温度信息
if self._class == "B16511":
self.driver.find_by_id('opt202610372').click()
elif self._class == "B16512":
self.driver.find_by_id('opt202610373').click()
if self._am_or_pm == "上午":
self.driver.find_by_id('opt202610374').click()
else:
self.driver.find_by_id('opt202610375').click()
self.driver.find_by_xpath('//*[@id="202896043"]/div[2]/div/div/input').fill(self._name)
self.driver.find_by_xpath('//*[@id="202896044"]/div[2]/div/div/input').fill(self._id)
self.driver.find_by_xpath('//*[@id="202896045"]/div[2]/div/div/input').fill(self._address)
self.driver.find_by_xpath('//*[@id="202896046"]/div[2]/div/div/div[1]/input').fill(self._date)
self.driver.find_by_xpath('//*[@id="202896048"]/div[2]/div/div/input').fill(self.temp)
self.driver.find_by_xpath('//*[@id="202896049"]/div[2]/div/div/input').fill(self._phone)
self.driver.find_by_xpath('//*[@id="202896050"]/div[2]/div/div/input').fill(self.condition)
self.driver.find_by_xpath('//*[@id="202896051"]/div[2]/div/div/input').fill(self.activitie)
time.sleep(1)
win32api.MessageBox(0, "请再次确认要提交的信息,无误后手动点击下方提交按钮", "提醒", win32con.MB_OKCANCEL)
# driver.find_by_id('form_submit').click() # 设置自动提交
print("提交信息成功")
except Exception as exc:
print("error:" + str(exc))
def _setDate(self):
self._date = time.strftime("%Y-%m-%d", time.localtime(time.time()))
hour = int(time.strftime("%H", time.localtime(time.time())))
if hour < 12:
self._am_or_pm = "上午"
elif hour >= 12:
self._am_or_pm = "下午"
print("当前时间:" + self._date + ", " + self._am_or_pm)
@staticmethod
def getResolution():
"""
获取屏幕的分辨率,以便最大化设置浏览器
@由于我的屏幕分辨率是缩放150%,所以我最后除了1.5,在我的电脑上感觉还可以。
"""
import winreg
import wmi
PATH = "SYSTEM\\ControlSet001\\Enum\\"
m = wmi.WMI()
# 获取屏幕信息
monitors = m.Win32_DesktopMonitor()
for m in monitors:
subPath = m.PNPDeviceID
infoPath = PATH + subPath + "\\Device Parameters"
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, infoPath)
# 屏幕信息按照一定的规则保存(EDID)
value = winreg.QueryValueEx(key, "EDID")[0]
winreg.CloseKey(key)
# 屏幕实际尺寸
# width, height = value[21], value[22]
# 推荐屏幕分辨率
widthResolution = value[56] + (value[58] >> 4) * 256
heightResolution = value[59] + (value[61] >> 4) * 256
# 屏幕像素密度(Pixels Per Inch)
# widthDensity = widthResolution / (width / 2.54)
# heightDensity = heightResolution / (height / 2.54)
return widthResolution / 1.5, heightResolution / 1.5
@staticmethod
def readconfig():
import configparser
import os
# 读取配置信息
cp = configparser.ConfigParser(allow_no_value=True)
# file_path = os.path.dirname(os.getcwd()) + '/config/config.ini'
# parpath = os.path.abspath('.') # 获取当前目录
# parpath = os.path.dirname(parpath) # 获取当前目录的上级目录
# CONFIG_PATH = parpath + '\\config.ini' # 拼接查询对应目录
# 上面三个可以合成这一个
CONFIG_PATH = os.path.abspath('.') + '\\conf.ini'
# 自动获取驱动的路径
global executable_path
executable_path = os.path.abspath('.') + '\\chromedriver.exe'
print(executable_path)
# 读取配置文件(此处是utf-8-sig,而不是utf-8),这里主要解决读取配置文件中文乱码问题
cp.read(CONFIG_PATH, encoding="utf-8-sig")
return cp['CONFIG']
if __name__ == '__main__':
temperature = Temperature()
temperature.start()