最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年03月31日 15:06:50   作者:Sqwlly  
這篇文章主要介紹了Python + selenium + crontab實(shí)現(xiàn)每日定時(shí)自動(dòng)打卡功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

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

安裝selenium庫

$ sudo pip install selenium

安裝chromdriver

因?yàn)槲矣写硭灾苯釉诠倬W(wǎng)下載的,那這里你可以選擇用淘寶鏡像源。

在這里插入圖片描述

這里為了方便,我直接放命令了。Chromedriver版本我這里選擇的是80.0.3987.16(注意要和一會(huì)兒下載的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

測(cè)試調(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') # 這個(gè)配置很重要
 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'開始進(jìn)行打卡'.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)站做對(duì)應(yīng)的修改。

腳本定時(shí)執(zhí)行

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

$ crontab -e

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

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

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

Chrome在服務(wù)器端運(yùn)行參考博文:http://m.fzitv.net/article/183899.htm

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

相關(guān)文章

  • python基于K-means聚類算法的圖像分割

    python基于K-means聚類算法的圖像分割

    這篇文章主要介紹了python基于K-means聚類算法的圖像分割,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python進(jìn)階之如何快速將變量插入有序數(shù)組

    Python進(jìn)階之如何快速將變量插入有序數(shù)組

    在我們學(xué)習(xí)python的過程中,學(xué)習(xí)序列是一門必修課。本文我們就來一起看一看Python是如何快速將變量插入有序數(shù)組的,感興趣的可以了解一下
    2023-04-04
  • python通過BF算法實(shí)現(xiàn)關(guān)鍵詞匹配的方法

    python通過BF算法實(shí)現(xiàn)關(guān)鍵詞匹配的方法

    這篇文章主要介紹了python通過BF算法實(shí)現(xiàn)關(guān)鍵詞匹配的方法,實(shí)例分析了BF算法的原理與Python實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Python語言中的if語句詳情

    Python語言中的if語句詳情

    這篇文章主要介紹了Python語言中的if語句詳情,每條if語句的核心都是一個(gè)值為True或False的表達(dá)式,這種表達(dá)式被稱為條件測(cè)試,下面文章介紹python語言的if語句詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-02-02
  • python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)

    python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)

    在Python中,要打印當(dāng)前路徑,可以使用os模塊中的getcwd()函數(shù),本文主要介紹了python樹狀打印項(xiàng)目路徑,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Python?requests下載文件的幾種常用方法(附代碼)

    Python?requests下載文件的幾種常用方法(附代碼)

    這篇文章主要介紹了五種下載方式的實(shí)現(xiàn)方法,包括基礎(chǔ)下載、大文件分塊下載、帶有斷點(diǎn)續(xù)傳的下載、帶有超時(shí)和重試的下載以及完整的下載器實(shí)現(xiàn),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2025-03-03
  • Tensorflow 實(shí)現(xiàn)將圖像與標(biāo)簽數(shù)據(jù)轉(zhuǎn)化為tfRecord文件

    Tensorflow 實(shí)現(xiàn)將圖像與標(biāo)簽數(shù)據(jù)轉(zhuǎn)化為tfRecord文件

    今天小編就為大家分享一篇Tensorflow 實(shí)現(xiàn)將圖像與標(biāo)簽數(shù)據(jù)轉(zhuǎn)化為tfRecord文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python的三目運(yùn)算符和not in運(yùn)算符使用示例

    python的三目運(yùn)算符和not in運(yùn)算符使用示例

    這篇文章主要介紹了python的三目運(yùn)算符和not in運(yùn)算符使用示例,需要的朋友可以參考下
    2014-03-03
  • pycharm運(yùn)行OpenCV項(xiàng)目踩坑記錄

    pycharm運(yùn)行OpenCV項(xiàng)目踩坑記錄

    本文主要介紹了在PyCharm中運(yùn)行OpenCV項(xiàng)目時(shí)遇到的問題及其解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • python圖片和二進(jìn)制轉(zhuǎn)換的三種實(shí)現(xiàn)方式

    python圖片和二進(jìn)制轉(zhuǎn)換的三種實(shí)現(xiàn)方式

    本文介紹了將PIL格式、數(shù)組和圖片轉(zhuǎn)換為二進(jìn)制的不同方法,包括使用PIL庫、OpenCV和直接讀取二進(jìn)制,此外,還提到了數(shù)據(jù)傳輸中base64格式的應(yīng)用,這些信息對(duì)需要進(jìn)行圖片數(shù)據(jù)處理和轉(zhuǎn)換的開發(fā)者非常有用
    2024-09-09

最新評(píng)論

白朗县| 武清区| 民县| 浑源县| 漳州市| 菏泽市| 昔阳县| 永吉县| 紫阳县| 东平县| 高淳县| 苍溪县| 右玉县| 临武县| 榆中县| 宁津县| 改则县| 临猗县| 宁安市| 大冶市| 丰顺县| 佛冈县| 苍溪县| 宣恩县| 安岳县| 策勒县| 沙雅县| 沧源| 喀什市| 台湾省| 昂仁县| 彭阳县| 白沙| 射洪县| 东兴市| 聊城市| 土默特左旗| 巴彦淖尔市| 永清县| 台安县| 牡丹江市|