python實(shí)現(xiàn)自動(dòng)打卡小程序
本文實(shí)例為大家分享了python實(shí)現(xiàn)自動(dòng)打卡小程序的具體代碼,供大家參考,具體內(nèi)容如下
"""
湖南大學(xué)疫情防控每日自動(dòng)打卡程序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.tmp_yesterday = tmp_yesterday
self.tmp_today = tmp_today
self.img_path = "captcha.png"
def get_captcha_img(self):
url = "https://fangkong.hnu.edu.cn/app/#/login?redirect=%2Fhome"
self.browser.get(url)
self.browser.find_element_by_class_name("vcdoe-tips").click() # 模擬點(diǎn)擊使驗(yàn)證碼加載出來(lái)
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 = requests.get(src)
if r.status_code == 200:
open(self.img_path, "wb").write(r.content)
else:
print("網(wǎng)絡(luò)不佳,無(wú)法加載驗(yàn)證碼圖片")
def recog_captcha_img(self):
img = Image.open(self.img_path)
img = img.convert('L') # P模式轉(zhuǎn)換為L(zhǎng)模式(灰度模式默認(rèn)閾值127)
count = 165 # 設(shè)定閾值
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) # 保存處理后的驗(yàn)證碼
## 調(diào)用百度ocr
# 識(shí)別碼
APP_ID = "23779944"
API_KEY = "FPgsSXsuqXk3twpqVHmNNK6g"
SECRET_KEY = "nG08oGzErk8CdMvDAynAiGdzfBjHr3NO"
# 初始化對(duì)象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
def get_file_content(file_path):
with open(file_path, 'rb') as f:
return f.read()
image = get_file_content(self.img_path)
# 定義參數(shù)變量
options = {'language_type': 'ENG', } # 識(shí)別語(yǔ)言類型,默認(rèn)為'CHN_ENG'中英文混合
# 調(diào)用通用文字識(shí)別
result = client.basicGeneral(image, options) # 高精度接口 basicAccurate
for word in result['words_result']:
self.captcha = (word['words'])
def login(self):
## 登錄
while True:
self.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 err_message.text == "登錄成功":
print("登錄成功!")
break
elif err_message.text == "賬號(hào)或密碼錯(cuò)誤":
print("賬號(hào)或密碼錯(cuò)誤!請(qǐng)重新輸入!")
self.stu_id = input("請(qǐng)輸入學(xué)號(hào):")
self.passwd = input("請(qǐng)輸入密碼:")
continue
else:
self.get_captcha_img()
self.recog_captcha_img()
continue
def attend(self):
success_messages = self.browser.find_elements_by_css_selector('p[class=el-message__content]')
success_messages = [message.text 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()="岳麓區(qū)"]').click()
time.sleep(1)
self.browser.find_element_by_xpath('//div/button[text()="確認(rèn)"]').click()
time.sleep(1)
## 輸入相關(guān)打卡信息并點(diǎn)擊打卡按鈕
self.browser.find_elements_by_css_selector('input[placeholder="街道門牌、樓層房間號(hào)等信息"]')[1].send_keys(self.address)
temp = self.browser.find_elements_by_css_selector("input[placeholder=請(qǐng)輸入]")
temp[0].send_keys(self.tmp_yesterday)
temp[1].send_keys(self.tmp_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 = datetime.now().strftime("%Y-%m-%d")
print(today + "打卡成功!")
time.sleep(60)
if __name__ == "__main__":
## 歡迎界面
print("=" * 100)
print("打卡小程序")
print("歡迎你湖南大學(xué)的朋友!開(kāi)始使用吧!")
print("=" * 100)
## 用戶輸入
while True:
t = input("請(qǐng)輸入你的每日打卡時(shí)間(24小時(shí)制, 例如:00:10):")
if re.search('^(([0-1][0-9])|(2[1-3])):[0-5][0-9]$', t) == None:
print("你輸入的時(shí)間格式有誤,請(qǐng)重新輸入!")
continue
stu_id = input("請(qǐng)輸入你的學(xué)號(hào):")
passwd = input("請(qǐng)輸入個(gè)人門戶密碼:")
address = input("請(qǐng)輸入你的打卡詳細(xì)地址(例如:湖南大學(xué)北校區(qū)1舍):")
tmp_yesterday = input("請(qǐng)輸入你的昨日體溫:")
tmp_today = input("請(qǐng)輸入你的今日體溫:")
print("=" * 100)
if input("請(qǐng)檢查你的輸入是否無(wú)誤,若有誤則輸入y并重新輸入,若無(wú)誤則輸入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
}
## 瀏覽器設(shè)置
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="chromedriver.exe", options=chrome_options)
## 打卡
app = DailyAttend(browser, **user_info) # 實(shí)例化打卡器
print("正在等待打卡時(shí)間到來(lái)...")
while True:
if datetime.now().strftime("%H:%M") == t:
app.get_captcha_img()
app.recog_captcha_img()
app.login()
app.attend()
else:
time.sleep(10)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python超有趣實(shí)例通過(guò)冒泡排序來(lái)實(shí)現(xiàn)LOL厄斐琉斯控槍
冒泡排序是一種簡(jiǎn)單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對(duì)相鄰的元素,當(dāng)該對(duì)元素順序不正確時(shí)進(jìn)行交換。一直重復(fù)這個(gè)過(guò)程,直到?jīng)]有任何兩個(gè)相鄰元素可以交換,就表明完成了排序2022-05-05
解決python調(diào)用自己文件函數(shù)/執(zhí)行函數(shù)找不到包問(wèn)題
這篇文章主要介紹了解決python調(diào)用自己文件函數(shù)/執(zhí)行函數(shù)找不到包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
淺談Pycharm最有必要改的幾個(gè)默認(rèn)設(shè)置項(xiàng)
今天小編就為大家分享一篇淺談Pycharm最有必要改的幾個(gè)默認(rèn)設(shè)置項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python 實(shí)現(xiàn)創(chuàng)建文件夾和創(chuàng)建日志文件的方法
這篇文章主要介紹了python 實(shí)現(xiàn)創(chuàng)建文件夾和創(chuàng)建日志文件的方法,文中給大家介紹了python 讀寫(xiě)創(chuàng)建文件文件夾的方法 ,需要的朋友可以參考下2019-07-07
Python本地搭建靜態(tài)Web服務(wù)器的實(shí)現(xiàn)
本文主要介紹了Python本地搭建靜態(tài)Web服務(wù)器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python基于opencv實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了python基于opencv實(shí)現(xiàn)人臉識(shí)別的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行步驟詳解
這篇文章主要介紹了關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行的步驟詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

