国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁/技術(shù)文章
文章詳情頁

Python + selenium + crontab實現(xiàn)每日定時自動打卡功能

瀏覽:9日期:2022-07-31 15:08:36

前言

近幾日迫于被輔導(dǎo)員三番五次的提醒每日一報打卡,就想著去寫個腳本掛在服務(wù)器上定時執(zhí)行。經(jīng)過我不懈的努力,最終選擇了seleniumseleniumselenium,因為簡單(

安裝selenium庫

$ sudo pip install selenium

安裝chromdriver

因為我有代理所以直接在官網(wǎng)下載的,那這里你可以選擇用淘寶鏡像源。

Python + selenium + crontab實現(xiàn)每日定時自動打卡功能

這里為了方便,我直接放命令了。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=utf8from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionimport timeclass 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í)行

這里我們利用LinuxLinuxLinux的內(nèi)置命令crontabcrontabcrontab,關(guān)于crontabcrontabcrontab的用法請自行百度ororor谷歌。

$ crontab -e

如果是首次使用,應(yīng)該會讓你選擇編輯器,我選擇的vimvimvim,然后在最后一行加入一行

0 0 * * * python ~/dailyReport.py >> ~/dailyReport.log

這樣就可以做到每天00:0000:0000:00自動執(zhí)行腳本了。

Chrome在服務(wù)器端運行參考博文:https://www.jb51.net/article/183899.htm

到此這篇關(guān)于Python + selenium + crontab實現(xiàn)每日定時自動打卡的文章就介紹到這了,更多相關(guān)python 定時自動打卡內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 成人欧美网站免费 | 在线观看日本永久免费视频 | 国产男女猛烈无遮档免费视频网站 | 国产在线视频自拍 | 免费久久精品 | 色怡红院 | 热99re久久精品2久久久 | 欧美成人毛片一级在线 | 日本精品在线观看 | 久久这里有精品视频 | 中文精品久久久久国产不卡 | 国产成人精品日本亚洲语音2 | 中文字幕日韩在线 | 就草草在线观看视频 | 日日摸人人拍人人澡 | 最新国产精品亚洲 | 欧美激情一区二区亚洲专区 | 成人国产在线观看 | 日本加勒比在线 | 99久久精品国产9999高清 | 91成人免费观看在线观看 | 国产年成美女网站视频免费看 | 日本黄色大片在线播放视频免费观看 | 99在线热视频 | 久草在线新视觉 | 国产成人免费全部网站 | 91久久另类重口变态 | 午夜久久视频 | 网站在线看 | 麻豆国产视频 | 成人免费视频一区 | 白白在线观看永久免费视频 | 91精品国产薄丝高跟在线看 | 久草福利在线观看 | 香港aa三级久久三级老师 | 91寡妇天天综合久久影院 | 免费在线看黄网址 | 国产区最新| 亚洲 中文 欧美 日韩 在线人 | 露脸 在线 国产 眼镜 | 日本午夜精品 |