python实现⾃动打卡⼩程序本⽂实例为⼤家分享了python实现⾃动打卡⼩程序的具体代码,供⼤家参考,具体内容如下"""
湖南⼤学疫情防控每⽇⾃动打卡程序v1.0
author: Liu
time:2021/3/16
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
from bs4 import BeautifulSoup
import requests
from aip import AipOcr
import time
from datetime import datetime
import re
class DailyAttend(object):
def __init__(self, browser, stu_id, passwd, t, address, tmp_yesterday, tmp_today):
self.browser = browser
self.stu_id = stu_id
self.passwd = passwd
self.t = t
self.address = address
self.img_path = "captcha.png"
def get_captcha_img(self):
url = "fangkong.hnu.edu/app/#/login?redirect=%2Fhome"
(url)
self.browser.find_element_by_class_name("vcdoe-tips").click() # 模拟点击使验证码加载出来
time.sleep(2)
webpage = self.browser.page_source
soup = BeautifulSoup(webpage, features="html.parser")
div = soup.find("div", attrs={"class": "login-content"})
src = div.find_all("img")[2].attrs["src"] # 从html中解析出图⽚链接
r = (src)
if r.status_code == 200:
open(self.img_path, "wb").t)
else:
print("⽹络不佳,⽆法加载验证码图⽚")
def recog_captcha_img(self):
img = Image.open(self.img_path)
img = vert('L') # P模式转换为L模式(灰度模式默认阈值127)
count = 165 # 设定阈值
table = []
for i in range(256):
if i < count:
table.append(0)
else:
table.append(1)
img = img.point(table, '1')
img.save(self.img_path) # 保存处理后的验证码
## 调⽤百度ocr
# 识别码
APP_ID = "23779944"
API_KEY = "FPgsSXsuqXk3twpqVHmNNK6g"
SECRET_KEY = "nG08oGzErk8CdMvDAynAiGdzfBjHr3NO"
# 初始化对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 读取图⽚
def get_file_content(file_path):
with open(file_path, 'rb') as f:
ad()
image = get_file_content(self.img_path)
# 定义参数变量
options = {'language_type': 'ENG', } # 识别语⾔类型,默认为'CHN_ENG'中英⽂混合
# 调⽤通⽤⽂字识别
result = client.basicGeneral(image, options) # ⾼精度接⼝ basicAccurate
for word in result['words_result']:
self.captcha = (word['words'])
def login(self):
## 登录
while True:
xpath语法 pythonself.browser.find_element_by_css_selector("[type=text]").send_keys(self.stu_id)
self.browser.find_element_by_css_selector("[type=password]").send_keys(self.passwd)
self.browser.find_element_by_css_selector("[type=number]").send_keys(self.captcha)
self.browser.find_element_by_tag_name("button").click()
time.sleep(2)
page = self.browser.page_source
html = BeautifulSoup(page, features="html.parser")
err_message = html.find("p", attrs={"class": "el-message__content"})
if == "登录成功":
print("登录成功!")
break
elif == "账号或密码错误":
print("账号或密码错误!请重新输⼊!")
self.stu_id = input("请输⼊学号:")
self.passwd = input("请输⼊密码:")
continue
else:
<_captcha_img()
<_captcha_img()
continue
def attend(self):
success_messages = self.browser.find_elements_by_css_selector('p[class=el-message__content]')
success_messages = [ for message in success_messages]
if "今⽇已打卡" in success_messages:
print("今⽇已打卡!")
time.sleep(60)
else:
## 选择打卡定位
self.browser.find_elements_by_xpath('//div/span[text()="正在获取定位..."]')[1].click()
time.sleep(1)
for i in range(17):
self.browser.find_elements_by_xpath('//ul/li')[i + 1].click()
time.sleep(1)
self.browser.find_element_by_xpath('//ul/li[text()="岳麓区"]').click()
time.sleep(1)
self.browser.find_element_by_xpath('//div/button[text()="确认"]').click()
time.sleep(1)
## 输⼊相关打卡信息并点击打卡按钮
self.browser.find_elements_by_css_selector('input[placeholder="街道门牌、楼层房间号等信息"]')[1].se
nd_keys(self.address)      temp = self.browser.find_elements_by_css_selector("input[placeholder=请输⼊]")
temp[0].send_p_yesterday)
temp[1].send_p_today)
self.browser.find_elements_by_css_selector(
'button[class="btnDaka van-button van-button--info van-button--normal van-button--block"]')[1].click()
today = w().strftime("%Y-%m-%d")
print(today + "打卡成功!")
time.sleep(60)
if __name__ == "__main__":
## 欢迎界⾯
print("=" * 100)
print("打卡⼩程序")
print("欢迎你湖南⼤学的朋友!开始使⽤吧!")
print("=" * 100)
## ⽤户输⼊
while True:
t = input("请输⼊你的每⽇打卡时间(24⼩时制, 例如:00:10):")
if re.search('^(([0-1][0-9])|(2[1-3])):[0-5][0-9]$', t) == None:
print("你输⼊的时间格式有误,请重新输⼊!")
continue
stu_id = input("请输⼊你的学号:")
passwd = input("请输⼊个⼈门户密码:")
address = input("请输⼊你的打卡详细地址(例如:湖南⼤学北校区1舍):")
tmp_yesterday = input("请输⼊你的昨⽇体温:")
tmp_today = input("请输⼊你的今⽇体温:")
print("=" * 100)
if input("请检查你的输⼊是否⽆误,若有误则输⼊y并重新输⼊,若⽆误则输⼊n:") == "n":
print("=" * 100)
break
user_info = {
't': t,
'stu_id': stu_id,
'passwd': passwd,
'address': address,
'tmp_yesterday': tmp_yesterday,
'tmp_today': tmp_today
}
## 浏览器设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(executable_path="", options=chrome_options)  ## 打卡
app = DailyAttend(browser, **user_info) # 实例化打卡器
print("正在等待打卡时间到来...")
while True:
w().strftime("%H:%M") == t:
<_captcha_img()
<_captcha_img()
app.login()
app.attend()
else:
time.sleep(10)
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。