易班web自動化打卡指令碼
一直以來,易班打卡被稱為最雞肋,最浪費時間,最浪費納稅人錢財的表面工程,那麼如何解決這一問題,已成為現在高校大學生的急需思考的大問題。那這一次我就來談談我是怎麼解決這一問題的。
分析眾所周知,學生雖然使用較多的是易班APP,但是,善良的易班還做了網頁端。剛好前不久我學習了一點兒web自動化技術,正好拿來練練手。 先來看看易班的登入介面,它使用了驗證碼,然而,圖形驗證碼就是專門剋制機器操作的,所以驗證碼只能使用人為識別。那該如何實現自動登入呢?
我們發現,登入一次之後,再次開啟頁面就會自動登入,原來易班記錄了使用者的cookies。 沒錯,你猜對了!我就是儲存cookies,每次登入都更換cookies,以實現多使用者登入打卡。
實現過程環境搭建網上有很多webdriver環境搭建的教程,我就不再囉嗦了
定位元素使用google瀏覽器複製的full xpath driver.find_element_by_xpath("/html/body/div1/div/div[2]/div1").click() 透過full xpath查詢網頁元素,並且執行點選操作;
driver.find_element_by_id(“account-txt”).send_keys("*****") 透過id查詢網頁元素,並且執行輸入操作;
會用這兩個方法就夠了(這也太low了)
獲取cookies一段非常簡單的程式碼,跑跑看
import selenium.webdriverimport timeimport jsonlst[] = ["",""] # 分別填上賬號和密碼driver = selenium.webdriver.Chrome()driver.implicitly_wait(10)driver.get("http://www.yiban.cn/")driver.delete_all_cookies() # 刪除已有cookiesprint("登入過程需要手動輸入驗證碼")time.sleep(1)driver.get("https://www.yiban.cn/login?go=http%3A%2F%2Fwww.yiban.cn%2F") # 進入登入介面driver.find_element_by_id("account-txt").send_keys(lst[0])driver.find_element_by_id("password-txt").send_keys(lst[1])driver.find_element_by_id("login-btn").click()print("登入過程可能需要手動輸入驗證碼\n完成登入後,鍵入y,開始提取使用者cookies\n無法登入情況下,鍵入n或其他任意字元,跳過此次提取")dd = input("是否完成登入?(y/n)")if(dd == 'y'): cookies = driver.get_cookies() print("已獲取cookies") jsoncookies = json.dumps(cookies) # 將cookies儲存到cookies.txt中 with open("cookies.txt", "a+")as f: ss = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) f.write("\n"+ss+"#"+str(lst[0])+"#"+str(lst[1])+"#"+"cookies--->"+jsoncookies) print("cookies已新增在cookies.txt!")else: print("未提取cookies,並跳過此使用者")driver.quit()
定時啟動指令碼假設看到這的小夥伴已經寫好了完整程式碼,現在開始用伺服器定時啟動打卡指令碼 crontab -e
3 6,11,17 * * * /usr/bin/python /home/yiban/yibandaka.py >> /www/wwwroot/mblog.ren/submit/yiban/run_log.txt 2>&1 每天的6點、11點、17點的03分開始執行指令碼,並將記錄儲存在run_log.txt
完整程式碼程式碼寫的比較隨意,將就一下吧! 下面程式碼是直接從伺服器複製過來的,用電腦的話,需要稍作修改
import selenium.webdriverimport timeimport randomimport jsonimport requestsdef daka(Tim,TempList): # 伺服器上要使用絕對路徑,電腦是可以用相對路徑;cookies_list.txt儲存了使用者cookies,每行一個cookies,比如:三個人就是三行cookies with open("/home/yiban/cookies_list.txt", "r", encoding="utf-8")as fl: kk = 0 dd = 0 lst=[] # 以下配置用於伺服器,電腦可使用下方註釋的的程式碼 option =selenium.webdriver.ChromeOptions() option.add_argument('--no-sandbox') option.add_argument('--headless') driver = selenium.webdriver.Chrome(executable_path='/home/software/chromedriver', chrome_options=option) # 注意path,我這裡是chromedriver放在/home/apk/chromedriver driver.get("http://www.yiban.cn/") driver.implicitly_wait(6) # 以下配置用於電腦,伺服器可使用上方的的程式碼 # driver = selenium.webdriver.Chrome() # driver.get("http://www.yiban.cn/") # driver.implicitly_wait(6) for line in fl.readlines(): if(dd == 8): print("| The website of yiban maybe have some problem !") break kk = kk + 1 cookies = json.loads(line) print("| %d Replace login cookies!\t" %kk , end="") driver.delete_all_cookies() driver.get("http://www.yiban.cn/") for co in cookies: driver.add_cookie(co) try: driver.get("https://f.yiban.cn/iapp659175") time.sleep(0.5) if(Tim == 1): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[1]").click() #google瀏覽器複製的full xpath,下面都是 time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() elif(Tim == 2): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() elif(Tim == 3): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[3]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() except: dd = dd + 1 print("%d execution failure! %d" % (kk,dd)) lst.append(line) else: print("%d execution successful!" % kk) driver.quit() # 打卡失敗者重新打卡 if(lst and dd < 8): print("|++++++++++++++++++Failure re-execution++++++++++++++++++") jj = 0 time.sleep(3) # 注意這裡配置是伺服器,電腦需要更換這裡的配置 option =selenium.webdriver.ChromeOptions() option.add_argument('--no-sandbox') option.add_argument('--headless') driver = selenium.webdriver.Chrome(executable_path='/home/software/chromedriver', chrome_options=option) driver.get("http://www.yiban.cn/") driver.implicitly_wait(6) # time.sleep(1) for lst1 in lst: jj = jj + 1 cookies = json.loads(lst1) print("|+ %d Replace login cookies!\t" %jj, end="") driver.delete_all_cookies() driver.get("http://www.yiban.cn/") for co in cookies: driver.add_cookie(co) try: driver.get("https://f.yiban.cn/iapp659175") time.sleep(1) if(Tim == 1): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[1]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() elif(Tim == 2): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[2]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() elif(Tim == 3): driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[3]").click() time.sleep(0.5) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").clear() tmp=TempList[random.randint(0,2)] driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[1]/div/input").send_keys(tmp) driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[3]/input").click() driver.find_element_by_xpath("/html/body/div[1]/div/div[5]/div/div[2]/div[4]/span").click() except: print("+ %d execution failure!" %jj) else: print("+ %d execution successful" %jj) driver.quit() if __name__ == "__main__": ss = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time())) print(" _______________________________________________________") print("|-----------------" + ss + "------------------") ss = time.strftime("%H", time.localtime(time.time())) # 判斷打卡時間,分別是6點,11點,17點 if(ss == "06"): daka(1,["36.6","36.7","36.8"]) elif(ss == "11"): daka(2,["36.7","36.8","36.9"]) elif(ss == "17"): daka(3,["36.6","36.7","36.8"]) ss = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time())) print("|_________________" + ss + "_______________over\n")
獲取cookies指令碼
import selenium.webdriverimport timeimport jsondriver = selenium.webdriver.Chrome()driver.implicitly_wait(10)driver.get("http://www.yiban.cn/")driver.delete_all_cookies()# shuzu.txt是用來存放賬號和密碼的,一行一個賬號和密碼,賬號和密碼用逗號隔開fs = open('shuzu.txt', 'r')num = fs.readlines()if len(num) <= 0: print("無內容")else: for i in num: driver.delete_all_cookies() time.sleep(1) driver.get("http://www.yiban.cn/") time.sleep(3) driver.delete_all_cookies() driver.get("https://www.yiban.cn/login?go=http%3A%2F%2Fwww.yiban.cn%2F") lst = i.strip().split(",") driver.find_element_by_id("account-txt").send_keys(lst[0]) driver.find_element_by_id("password-txt").send_keys(lst[1]) driver.find_element_by_id("login-btn").click() print("登入過程可能需要手動輸入驗證碼\n完成登入後,鍵入y,開始提取使用者cookies\n無法登入情況下,鍵入n或其他任意字元,跳過此次提取") dd = input("是否完成登入?(y/n)") if(dd == 'y'): time.sleep(1) cookies = driver.get_cookies() jsoncookies = json.dumps(cookies) with open("cookies.txt", "a+")as f: ss = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) f.write("\n"+ss+"#"+str(lst[0])+"#"+str(lst[1])+"#"+"cookies--->"+jsoncookies) print("cookies已新增在cookies.txt!") else: print("未提取cookies,並跳過此使用者")fs.close()driver.quit():後記
近期有很多朋友透過私信諮詢有關Python學習問題。為便於交流,後臺私信小編01即可