溫馨提示×

溫馨提示×

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

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

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

發(fā)布時間:2021-05-07 14:14:22 來源:億速云 閱讀:368 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Python + selenium + crontab怎么實現(xiàn)每日定時自動打卡功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

python的五大特點是什么

python的五大特點:1.簡單易學(xué),開發(fā)程序時,專注的是解決問題,而不是搞明白語言本身。2.面向?qū)ο?,與其他主要的語言如C++和Java相比, Python以一種非常強大又簡單的方式實現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無需修改就可以在各種平臺上運行。4.解釋性,Python語言寫的程序不需要編譯成二進制代碼,可以直接從源代碼運行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。

安裝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=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í)行

這里我們利用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í)行腳本了。

關(guān)于“Python + selenium + crontab怎么實現(xiàn)每日定時自動打卡功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向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