溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么通過Python實現(xiàn)定時打卡小程序

發(fā)布時間:2022-03-03 15:09:59 來源:億速云 閱讀:201 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“怎么通過Python實現(xiàn)定時打卡小程序”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“怎么通過Python實現(xiàn)定時打卡小程序”這篇文章吧。

只需在自己的python項目下隨便創(chuàng)建一個文件夾(下圖中為:daka),然后將下載的chromedriver.exe、ask_for_leave.py、log.txt(此文件夾為空,保存運行程序時的日志信息,直接在文件夾下創(chuàng)建一個名為log.txt的文件夾即可)。

怎么通過Python實現(xiàn)定時打卡小程序

chromedriver.exe

此文件是google瀏覽器的驅(qū)動文件,可在下載地址上選擇與自己電腦上的google瀏覽器相同版本的驅(qū)動。

如何查看google瀏覽器版本

第一步:打開Chrome瀏覽器

第二步:點擊右上角三個點,選擇“設(shè)置”

怎么通過Python實現(xiàn)定時打卡小程序

第三步:點擊“關(guān)于Chrome”

怎么通過Python實現(xiàn)定時打卡小程序

第四步:得到Chrome版本號

怎么通過Python實現(xiàn)定時打卡小程序

ask_for_leave.py(只需修改標注修改的兩個地方)

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import datetime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
def qinjia(browser,url):
    browser.get(url)
    sleep(1)
    browser.implicitly_wait(3)
    WebDriverWait(browser,5).until(EC.presence_of_all_elements_located((By.ID,"user_main")))
    user_main_div=browser.find_element_by_id("user_main")
    username_input=user_main_div.find_element_by_id("txtId")   #用戶名
    password_input=user_main_div.find_element_by_id("txtMM")    #密碼
    login_btn=user_main_div.find_element_by_id("IbtnEnter")   #登錄按鈕

    # 修改1:此處的賬號和密碼
    username_input.send_keys("==================賬號===================")
    password_input.send_keys("==================密碼===================")
    login_btn.click()

    sleep(1)
    browser.implicitly_wait(3)
    WebDriverWait(browser, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "tabThinM")))
    table_tag=browser.find_element_by_class_name("tabThinM")
    href_body=table_tag.find_elements_by_tag_name("tbody")[2]
    href=href_body.find_elements_by_tag_name("tr")[1].find_element_by_tag_name("a").get_attribute("href")
    browser.get(href)

    table_wjTA=browser.find_element_by_id("wjTA")

    div_gerenjiankang=table_wjTA.find_elements_by_class_name("dvO")[0]  #個人健康
    div_shenqing=table_wjTA.find_elements_by_class_name("dvO")[1]       #申請進入

    # 個人健康
    selects_tag=div_gerenjiankang.find_elements_by_tag_name("select")
    work_station_select=selects_tag[2]
    health_station_select=selects_tag[3]
    live_station_select=selects_tag[4]
    family_station_select=selects_tag[5]

    Select(work_station_select).select_by_value("1")
    Select(health_station_select).select_by_value("1")
    Select(live_station_select).select_by_value("1")
    Select(family_station_select).select_by_value("1")


    #申請進入
    select_shenqin_time_tags=div_shenqing.find_elements_by_tag_name("select")

    input_shenqin_reaseons_tags=div_shenqing.find_elements_by_tag_name("input")
    target_place_input=input_shenqin_reaseons_tags[0]
    reason_input=input_shenqin_reaseons_tags[1]
    # 修改2:成此處的申請目的地和事由
    target_place_input.send_keys("=====================申請目的========================")
    reason_input.send_keys("==========================事由===============================")

    Select(select_shenqin_time_tags[0]).select_by_value("1")
    Select(select_shenqin_time_tags[1]).select_by_value("06")
    Select(select_shenqin_time_tags[2]).select_by_value("3")
    Select(select_shenqin_time_tags[3]).select_by_value("23")

    submit_input=browser.find_element_by_tag_name("input")
    submit_input.click()


def log(message):
    curent_time = datetime.datetime.now()
    print(curent_time)

    f = open("log.txt", "a+", encoding="utf-8")

    f.write(str(curent_time) + ":  "+message+"\n")
    f.close()
def headLessChrome():
    chrome_driver = r"chromedriver.exe"
    chrome_options=Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    browser=webdriver.Chrome(options=chrome_options, executable_path=chrome_driver)
    return browser
if __name__ == '__main__':
    url="http://login.cuit.edu.cn/Login/xLogin/Login.asp"
    browser=headLessChrome()
    try:
        qinjia(browser,url)
        log("成功")
    except:
        log("失敗")
    browser.quit()

log.txt

直接創(chuàng)建一個空的log.txt文件。

創(chuàng)建完畢之后,run一下ask_for_leave.py文件,即可運行一次。

以上是“怎么通過Python實現(xiàn)定時打卡小程序”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI