?亥末,庚子春,荆楚疫,万人疾,九州震,街无华灯,路无舟车,万巷空寂,然,大夫、官员、将士英勇逆行而战也,布衣,商贾慷慨尽倾囊相助也,医者仁心悬壶济世也,与庚子年上元节后,荆楚之战胜之,疫去,国安,定后思之,将来吾如若得一子、吾辈当教子以科学,为国之崛起而读书,愿子腹有诗书,不随娘亲空口无补,于是呼“武汉加油、中国加油”。
?最近由于?δ毒株的肆虐,于是有了下面这一幕
大家都挺忙的,于是我想起了去年写的一个打卡程序,把它拿出来改改正好派上用场,话不多说,直接进入正题。
1.环境配置
1.相关库的安装
pip install selenium
pip install pymysql
pip install smtplib
2.下载Chrome谷歌浏览器对应版本的驱动:? Chrome? Drive,下载后解压至Python安装目录下的Scripts目录下
2.导入所需的库
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import pymysql
?3.登陆以及打卡(由于暂时不需要验证码,所以难度大大降低)
?从MySQL获取已经存储好的账号、密码以及用户邮箱
host = '127.0.0.1' #从MySQL获取账号密码
port = 3306
username = 'id'
password = 'password'
db = 'health_sign'
charset = 'utf8'
conn = pymysql.Connect(host=host,port=port,user=username,password=password,db=db,charset=charset)
cursor = conn.cursor()
sql= "select user_name, password, email from user_id"
try:
cursor.execute(sql)
user_list=cursor.fetchall()
except:
?控制浏览器登入打卡界面
for user in user_list:
days = 0
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
## driver = webdriver.Chrome() #有界面模式
driver.get("http://login.cuit.edu.cn/Login/xLogin/Login.asp")
time.sleep(2)
input=driver.find_element_by_id("txtId")
input.send_keys(user[0])#账号
input=driver.find_element_by_id("txtMM")
input.send_keys(user[1])#密码
element= driver.find_element_by_id("IbtnEnter")
element.click()
通过打√的次数可以获得用户连续打卡的天数
for dayspath in range(5,32):#获取连续打卡天数
if(driver.find_element_by_xpath('/html/body/div[2]/table/tbody[2]/tr[{}]/td[1]'.format(dayspath)).text=='√'):
days=days+1
else:
break
通过命名规则不难发现是当天的日期加上固定的字符,通过获取当天日期轻松找到我们需要点击的地方
locatime=time.localtime(time.time())
s='{:02}{:02}'.format(locatime.tm_mon,locatime.tm_mday)#获取当天日期
datename=s+'疫情防控——师生健康状态采集'
element=driver.find_element_by_link_text(datename)
element.click()
进入打卡页面后,第一条的现居住地是通过js默认获取的,我们可以以此来判断用户所在地(第三个选项)
根据需要选择或填写数据
state = driver.find_element_by_name("sF21650_1").get_attribute('value')#这部分为个人健康状况
if (state!='1'):
Select(driver.find_element_by_name("sF21650_6")).select_by_value("5")
else:
Select(driver.find_element_by_name("sF21650_6")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_7")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_8")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_9")).select_by_value("1")
#下面注释这部分为请假,如不需要请假则注释掉
## input=driver.find_element_by_name("sF21912_1")
## input.send_keys('北街')#请假目的地
## input=driver.find_element_by_name("sF21912_2")
## input.send_keys('吃饭')#请假事由
## Select(driver.find_element_by_name("sF21912_3")).select_by_value("1")#请假时间(1是今天,2是明天,3是后天)
## Select(driver.find_element_by_name("sF21912_4")).select_by_value("09")#出校时间(例如07代表7.00)
## Select(driver.find_element_by_name("sF21912_5")).select_by_value("3")#返校时间(1是当天,2是第二天,3是第三天,9是当晚)
## Select(driver.find_element_by_name("sF21912_6")).select_by_value("20")#进校时间(例如07代表7.00)
## print(s[i]+'请假成功!')
element=driver.find_element_by_name("B2")
element.click()
nowtime = time.localtime(time.time())
print(name+'打卡成功!'+'\n')
time.sleep(2)
driver.switch_to.alert.accept()
driver.close()
4.发送打卡成功邮件?
向用户的邮箱发送邮件提示打卡成功
try:
content = ("""
<html>
<head>
<meta charset="utf-8">
<title>疫情防控-健康打卡</title>
</head>
<body>
<h3 style="text-align:center">{}</h3>
<h1 style="text-align:center">今日状态:{}</h1>
<h1 style="text-align:center">您已连续打卡{}天</h1>
<h4 style="text-align:center">打卡时间:{}年{}月{}日{}:{}:{}</h4>
<h5 style="text-align:center"><a href="http://login.cuit.edu.cn/Login/xLogin/Login.asp" >点击此处可查看详情</a></h5>
</body>
</html>
""".format(datename,area[int(state)],days+1,nowtime.tm_year,nowtime.tm_mon,nowtime.tm_mday,nowtime.tm_hour,nowtime.tm_min,nowtime.tm_sec))
message = MIMEText(content, 'html', 'utf-8')
message['From'] = Header("疫情防控-健康打卡", 'utf-8')
message['To'] = Header("{}".format(name), 'utf-8')
subject = '{},您已打卡成功!'.format(name)
message['Subject'] = Header(subject, 'utf-8')
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是465,固定的,不能更改
server.login(sender, mail_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.set_debuglevel(1)
server.sendmail(sender, user[2], message.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
print('{}发送成功'.format(name))
except:
print('{}未发送成功'.format(name))
附上完整程序:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import pymysql
host = '127.0.0.1' #从MySQL获取账号密码
port = 3306
username = 'id'
password = 'password'
db = 'health_sign'
charset = 'utf8'
conn = pymysql.Connect(host=host,port=port,user=username,password=password,db=db,charset=charset)
cursor = conn.cursor()
sql= "select user_name, password, email from user_id"
try:
cursor.execute(sql)
user_list=cursor.fetchall()
except:
pass
mail_pass = "key" # 口令
sender = 'xxxxxxxx@qq.com'
area=['','航空港校内','龙泉校内','新气象小区','成信家园','成都(校外)','外地']
locatime=time.localtime(time.time())
s='{:02}{:02}'.format(locatime.tm_mon,locatime.tm_mday)#获取当天日期
datename=s+'疫情防控——师生健康状态采集'
for user in user_list:
days = 0
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
## driver = webdriver.Chrome()
driver.get("http://login.cuit.edu.cn/Login/xLogin/Login.asp")
time.sleep(2)
input=driver.find_element_by_id("txtId")
input.send_keys(user[0])#账号
input=driver.find_element_by_id("txtMM")
input.send_keys(user[1])#密码
element= driver.find_element_by_id("IbtnEnter")
element.click()
for dayspath in range(5,32):#获取连续打卡天数
if(driver.find_element_by_xpath('/html/body/div[2]/table/tbody[2]/tr[{}]/td[1]'.format(dayspath)).text=='√'):
days=days+1
else:
break
time.sleep(1)
element=driver.find_element_by_link_text(datename)
element.click()
time.sleep(1)
name = driver.find_element_by_css_selector("body > form > div:nth-child(1) > table > tbody > tr > td:nth-child(2) > p").text.split('(')[1].split(')')[0] #获取用户姓名
state = driver.find_element_by_name("sF21650_1").get_attribute('value')#这部分为个人健康状况
if (state!='1'):
Select(driver.find_element_by_name("sF21650_6")).select_by_value("5")
else:
Select(driver.find_element_by_name("sF21650_6")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_7")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_8")).select_by_value("1")
Select(driver.find_element_by_name("sF21650_9")).select_by_value("1")
#下面注释这部分为请假,如不需要请假则注释掉
## input=driver.find_element_by_name("sF21912_1")
## input.send_keys('北街')#请假目的地
## input=driver.find_element_by_name("sF21912_2")
## input.send_keys('吃饭')#请假事由
## Select(driver.find_element_by_name("sF21912_3")).select_by_value("1")#请假时间(1是今天,2是明天,3是后天)
## Select(driver.find_element_by_name("sF21912_4")).select_by_value("09")#出校时间(例如07代表7.00)
## Select(driver.find_element_by_name("sF21912_5")).select_by_value("3")#返校时间(1是当天,2是第二天,3是第三天,9是当晚)
## Select(driver.find_element_by_name("sF21912_6")).select_by_value("20")#进校时间(例如07代表7.00)
## print(s[i]+'请假成功!')
element=driver.find_element_by_name("B2")
element.click()
nowtime = time.localtime(time.time())
print(name+'打卡成功!'+'\n')
time.sleep(2)
driver.switch_to.alert.accept()
driver.close()
try:
content = ("""
<html>
<head>
<meta charset="utf-8">
<title>疫情防控-健康打卡</title>
</head>
<body>
<h3 style="text-align:center">{}</h3>
<h1 style="text-align:center">今日状态:{}</h1>
<h1 style="text-align:center">您已连续打卡{}天</h1>
<h4 style="text-align:center">打卡时间:{}年{}月{}日{}:{}:{}</h4>
<h5 style="text-align:center"><a href="http://login.cuit.edu.cn/Login/xLogin/Login.asp" >点击此处可查看详情</a></h5>
</body>
</html>
""".format(datename,area[int(state)],days+1,nowtime.tm_year,nowtime.tm_mon,nowtime.tm_mday,nowtime.tm_hour,nowtime.tm_min,nowtime.tm_sec))
message = MIMEText(content, 'html', 'utf-8')
message['From'] = Header("疫情防控-健康打卡", 'utf-8')
message['To'] = Header("{}".format(name), 'utf-8')
subject = '{},您已打卡成功!'.format(name)
message['Subject'] = Header(subject, 'utf-8')
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是465,固定的,不能更改
server.login(sender, mail_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.set_debuglevel(1)
server.sendmail(sender, user[2], message.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
print('{}发送成功'.format(name))
except:
print('{}未发送成功'.format(name))
# exit(0)
如有不足或错误,望各位大佬指点 。
|