Python + selenium + crontab實現(xiàn)每日定時自動打卡功能
前言
近幾日迫于被輔導(dǎo)員三番五次的提醒每日一報打卡,就想著去寫個腳本掛在服務(wù)器上定時執(zhí)行。經(jīng)過我不懈的努力,最終選擇了
安裝selenium庫
$ sudo pip install selenium
安裝chromdriver
因為我有代理所以直接在官網(wǎng)下載的,那這里你可以選擇用淘寶鏡像源。
這里為了方便,我直接放命令了。Chromedriver版本我這里選擇的是80.0.3987.16(注意要和一會兒下載的Chrome版本一致)。
下載
$ wget https://npm.taobao.org/mirrors/chromedriver/80.0.3987.16/chromedriver_linux64.zip
解壓
$ unzip chromedriver_linux64.zip -d .
放到相應(yīng)目錄并授予可執(zhí)行權(quán)限
$ sudo cp chromedriver /usr/bin && sudo chmod +x /usr/bin/chromedriver
安裝Chrome安裝依賴
$ sudo apt-get install libxss1 libappindicator1 libindicator7
安裝Chrome
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb$ sudo dpkg -i google-chrome*.deb$ sudo apt-get install -f
查看版本
$ google-chrome --version
測試調(diào)試
$ google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu
編寫腳本創(chuàng)建腳本并授予權(quán)限
$ touch dailyReport.py && touch dailyReport.log && sudo chmod +x dailyReport.py
內(nèi)容
# encoding=utf8 from selenium import webdriver from selenium.common.exceptions import NoSuchElementException import time class DailyReport(object): def __init__(self): self.chrome_options = webdriver.ChromeOptions() self.chrome_options.add_argument('--headless') self.chrome_options.add_argument('--disable-gpu') self.chrome_options.add_argument('--no-sandbox') # 這個配置很重要 self.client = None # self.client = webdriver.Chrome(chrome_options=self.chrome_options) self.index_url = 'https://xxxxx/xxxx/login' self.report_url = 'https://xxxx/xxxx/report' self.data = [ ('用戶名', '密碼'), ('xxxx', 'xxxx'), ('xxxx', 'xxxx'), ('xxxx', 'xxxx'), ('xxxx', 'xxxx') ] def login(self, _username, _password): try: self.client = webdriver.Chrome(chrome_options=self.chrome_options) print(self.get_current_time() + ' ' + _username + u'開始進行打卡'.encode('utf-8')) self.client.get(self.index_url) username = self.client.find_element_by_name("username") password = self.client.find_element_by_name('password') username.send_keys(_username) password.send_keys(_password) login_button = self.client.find_element_by_xpath('//*[@id="form1"]/div[4]/button') login_button.click() except NoSuchElementException: print(self.get_current_time(), u'登錄異常!'.encode('utf-8')) else: # time.sleep(2) print(self.get_current_time() + ' ' + u'登錄成功!'.encode('utf-8')) def post_data(self): try: self.client.get(self.report_url) submit_button = self.client.find_element_by_xpath('//*[@id="p1_ctl00_btnSubmit"]/span/span') submit_button.click() ensure_button = self.client.find_element_by_xpath('//*[@id="fineui_26"]/span/span') ensure_button.click() # print (client.page_source.encode('utf-8')) except NoSuchElementException: print(self.get_current_time(), u' 提交表單異常! 打卡失敗!'.encode('utf-8')) else: # time.sleep(2) print(self.get_current_time() + ' ' + u'打卡成功!\n'.encode('utf-8')) finally: time.sleep(5) self.client.quit() print(u'瀏覽器退出...\n--------------\n'.encode('utf-8')) def run(self): for msg in self.data: self.login(msg[0], msg[1]) self.post_data() print('Python script completed at ' + self.get_current_time() + '\n--------------\n') @staticmethod def get_current_time(): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if __name__ == '__main__': obj = DailyReport() obj.run()
腳本內(nèi)容需要根據(jù)不同網(wǎng)站做對應(yīng)的修改。
腳本定時執(zhí)行
這里我們利用
$ crontab -e
如果是首次使用,應(yīng)該會讓你選擇編輯器,我選擇的
0 0 * * * python ~/dailyReport.py >> ~/dailyReport.log
這樣就可以做到每天
注
Chrome在服務(wù)器端運行參考博文:http://www.dbjr.com.cn/article/183899.htm
到此這篇關(guān)于Python + selenium + crontab實現(xiàn)每日定時自動打卡的文章就介紹到這了,更多相關(guān)python 定時自動打卡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python實現(xiàn)自動打卡小程序
- Python實現(xiàn)釘釘/企業(yè)微信自動打卡的示例代碼
- 基于python+selenium自動健康打卡的實現(xiàn)代碼
- python實現(xiàn)自動打卡的示例代碼
- python+selenium 簡易地疫情信息自動打卡簽到功能的實現(xiàn)代碼
- Python 實現(xiàn)網(wǎng)課實時監(jiān)控自動簽到、打卡功能
- Python實現(xiàn)i人事自動打卡的示例代碼
- python獲取微信企業(yè)號打卡數(shù)據(jù)并生成windows計劃任務(wù)
- 使用Python實現(xiàn)企業(yè)微信的自動打卡功能
- Python實戰(zhàn)之單詞打卡統(tǒng)計
相關(guān)文章
Pycharm安裝第三方庫、安裝位置以及鏡像設(shè)置方法詳解
對于Python開發(fā)用戶來講,安裝第三方庫是家常便飯,下面這篇文章主要給大家介紹了關(guān)于Pycharm安裝第三方庫、安裝位置以及鏡像設(shè)置方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05詳解TensorFlow訓(xùn)練網(wǎng)絡(luò)兩種方式
本文主要介紹了TensorFlow訓(xùn)練網(wǎng)絡(luò)兩種方式,一種是基于tensor(array),另外一種是迭代器,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12selenium + python 獲取table數(shù)據(jù)的示例講解
今天小編就為大家分享一篇selenium + python 獲取table數(shù)據(jù)的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python如何實現(xiàn)int函數(shù)的方法示例
int()函數(shù)常用來把其他類型轉(zhuǎn)換為整數(shù),下面這篇文章主要給大家介紹了關(guān)于python如何實現(xiàn)int函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02TensorFlow可視化工具TensorBoard默認(rèn)圖與自定義圖
這篇文章主要介紹了TensorFlow可視化工具TensorBoard默認(rèn)圖與自定義圖的使用操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10Django中數(shù)據(jù)在前后端傳遞的方式之表單、JSON與ajax
Django從后臺往前臺傳遞數(shù)據(jù)時有多種方法可以實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Django中數(shù)據(jù)在前后端傳遞的方式之表單、JSON與ajax的相關(guān)資料,需要的朋友可以參考下2022-10-10Python中的TfidfVectorizer參數(shù)使用解析
這篇文章主要介紹了Python中的TfidfVectorizer參數(shù)使用解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11