網站首頁 編程語言 正文
1、安裝谷歌瀏覽器
#下載包 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb #安裝包,用deb方式安裝 sudo dpkg -i --force-depends google-chrome-stable_current_amd64.deb #######################分割############################### #若出現Packet xxx is not installed,代表依賴出現問題執行以下命令 apt-get install -f #重新執行一遍 sudo dpkg -i --force-depends google-chrome-stable_current_amd64.deb #######################分割############################## #為了讓其能夠無界面運行 sudo apt-get install xvfb
2、安裝瀏覽器驅動
#首先獲取chromedriver的最新版本信息 LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) #下載 wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip #解壓 unzip chromedriver_linux64.zip #如果沒有安裝unzip就運行 apt install unzip #權限配置 chmod +x chromedriver #移動到bin目錄下 sudo mv chromedriver /usr/bin/
3、安裝selenium
#看自己系統是不是pip3,-i表示用什么源下載 pip3 install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
4、簡單測試
首先創建test.py文件,之后執行python3 test.py
,查看效果
#coding=utf-8 from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_opt = Options() # 創建參數設置對象. chrome_opt.add_argument('--headless') # 無界面化. chrome_opt.add_argument('--disable-gpu') # 配合上面的無界面化. chrome_opt.add_argument('--window-size=1366,768') # 設置窗口大小, 窗口大小會有影響. chrome_opt.add_argument("--no-sandbox") #使用沙盒模式運行 # 創建Chrome對象并傳入設置信息. browser = webdriver.Chrome(chrome_options=chrome_opt) url = "https://www.baidu.com/" browser.get(url) print(browser.page_source) browser.quit()
5、打卡程序
這里我先用瀏覽器插件的Selenium先點擊好導出python文件進行修改。程序需要創建data.csv
文件,并在其中寫入賬號密碼
# Generated by Selenium IDE import sys import time from selenium import webdriver from selenium.webdriver.common.by import By # 讀取數據函數 userList = [] def readData(): with open("data.csv","r") as f: for lines in f.readlines(): data = lines.strip().split(",") userList.append(data) # 全局函數 def printLog(info): print('{0} {1}'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),info)) # 打卡類 class Test: # 初始化瀏覽器 def __init__(self): # 添加 Chrome 相關配置 options = webdriver.ChromeOptions() # 指定設備名稱即可 options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'}) options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') # options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=options) # => 打開瀏覽器時加入配置 printLog("瀏覽器初始化完成") # 正式打卡 def signIn(self,username,password): printLog("正在打開網頁") self.driver.get("http://stu.zstu.edu.cn/webroot/decision/url/mobile?origin=53fd9573-139e-4e3b-9357-4d791849ad58#/login") # 睡眠控制速度 # 刷新頁面 self.driver.refresh() time.sleep(3) self.driver.set_window_size(800, 824) self.driver.find_element(By.XPATH, "http://*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").click() self.driver.find_element(By.XPATH, "http://*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").send_keys(username) self.driver.find_element(By.XPATH, "http://input[@type=\'password\']").click() self.driver.find_element(By.XPATH, "http://input[@type=\'password\']").send_keys(password) time.sleep(3) self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(4)").click() time.sleep(4) printLog("登錄成功") # 尋找打卡功能模塊 self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(2)").click() time.sleep(2) printLog("開始打卡") self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_6 .css-901oao").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[34]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_4_row_6 > div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[2]/div/div[2]/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_5_row_6 > div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[2]/div/div[6]/div/div/div").click() time.sleep(1) self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_7 .css-1cwyjr8").click() self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_7 .css-1cwyjr8").send_keys("浙江理工大學") printLog("打卡進行中") #選項,不知道為什么css定位不能用 self.driver.find_element(By.XPATH, "http://div[10]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[12]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[13]/div/div/div/div/div/div/div[2]").click() self.driver.find_element(By.XPATH, "http://div[17]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[18]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[19]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[20]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[21]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[23]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[25]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[26]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[28]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[30]/div/div/div/div/div[2]/div/div").click() time.sleep(1) #提交 self.driver.find_element(By.CSS_SELECTOR, "#col_1_row_39").click() time.sleep(10) printLog("本次打卡成功") def quit(self): self.driver.quit() printLog("瀏覽器退出") if __name__ == '__main__': test = Test() readData() for i in range(len(userList)): try: test.signIn(userList[i][0],userList[i][1]) except: printLog("打卡成功或者系統bug,正在進行下一個") continue test.quit() printLog("今日打卡任務完成") sys.exit()
6、linux設置定時任務
#上傳代碼后,設置定時 crontab -e #打開后添加以下記錄,代表每天3點執行 0 3 * * * /usr/bin/python3 /home/shawn/ezl/sign.py > /home/shawn/ezl/elog.log 2>&1 #重啟 systemctl restart cron.service
7、其他
上面部分為研究生打開,下面為本科生
# Generated by Selenium IDE import sys import time from selenium import webdriver from selenium.webdriver.common.by import By # 讀取數據函數 userList = [] def readData(): with open("data.csv","r") as f: for lines in f.readlines(): data = lines.strip().split(",") userList.append(data) # 全局函數 def printLog(info): print('{0} {1}'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),info)) # 打卡類 class Test: # 初始化瀏覽器 def __init__(self): # 添加 Chrome 相關配置 options = webdriver.ChromeOptions() # 指定設備名稱即可 options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'}) options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=options) # => 打開瀏覽器時加入配置 printLog("瀏覽器初始化完成") # 正式打卡 def signIn(self,username,password): printLog("正在打開網頁") self.driver.get("http://stu.zstu.edu.cn/webroot/decision/url/mobile?origin=53fd9573-139e-4e3b-9357-4d791849ad58#/login") # 睡眠控制速度 # 刷新頁面 self.driver.refresh() time.sleep(3) self.driver.set_window_size(800, 824) self.driver.find_element(By.XPATH, "http://*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").click() self.driver.find_element(By.XPATH, "http://*[@id='app']/div/div[1]/div/div/div/div/div/div/div/div[2]/div[2]/div[1]/div/input").send_keys(username) self.driver.find_element(By.XPATH, "http://input[@type=\'password\']").click() self.driver.find_element(By.XPATH, "http://input[@type=\'password\']").send_keys(password) time.sleep(3) self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(4)").click() time.sleep(4) printLog("登錄成功") # 尋找打卡功能模塊 # self.driver.find_element(By.CSS_SELECTOR,".r-1loqt21:nth-child(2)").click() self.driver.find_element(By.XPATH, "http://*[@id='app']/div/div[1]/div/div/div/div[1]/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div[3]/div/div/div[5]/div/div/div[3]").click() time.sleep(5) printLog("開始打卡") # self.driver.find_element(By.CSS_SELECTOR, "#col_3_row_6 .css-901oao").click() self.driver.find_element(By.XPATH, "http://*[@id='col_1_row_11']/span").click() time.sleep(5) self.driver.find_element(By.XPATH, "http://div[2]/div/div/div/div/div/div/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[34]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[2]/div/div/div/div/div/div/div/div/div/div[2]/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[2]/div/div[2]/div/div/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[3]/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://div[6]/div/div/div/div").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://input").click() time.sleep(1) self.driver.find_element(By.XPATH, "http://input").send_keys("浙江理工大學") time.sleep(1) printLog("打卡進行中") #選項,不知道為什么css定位不能用 self.driver.find_element(By.XPATH, "http://div[11]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[13]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[14]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[18]/div/div/div/div/div/div/div[2]").click() self.driver.find_element(By.XPATH, "http://div[19]/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[19]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[20]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[21]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[22]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[24]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[26]/div/div/div/div/div/div/div").click() self.driver.find_element(By.XPATH, "http://div[27]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[29]/div/div/div/div/div[2]/div/div").click() self.driver.find_element(By.XPATH, "http://div[31]/div/div/div/div/div[2]/div/div").click() time.sleep(1) #提交 self.driver.find_element(By.XPATH, "http://*[@id='col_0_row_40']/div/div/div/div").click() # self.driver.find_element(By.CSS_SELECTOR, "#col_0_row_40 .r-1loqt21 > .css-1dbjc4n").click() time.sleep(10) printLog("本次打卡成功") def quit(self): self.driver.quit() printLog("瀏覽器退出") if __name__ == '__main__': test = Test() readData() for i in range(len(userList)): try: test.signIn(userList[i][0],userList[i][1]) except: printLog("打卡成功或者系統bug,正在進行下一個") continue test.quit() printLog("今日打卡任務完成") sys.exit()
=已經失效,僅做參考=
原文鏈接:https://blog.csdn.net/lemon_TT/article/details/124675419
- 上一篇:C#實現進制轉換_C#教程
- 下一篇:python如何輸入根號_python
相關推薦
- 2022-02-22 用Go+Redis實現分布式鎖的示例代碼_Golang
- 2022-09-29 關于react?ant?組件?Select下拉框?值回顯的問題_React
- 2022-07-04 Python如何一行輸入多個數,并存入列表_python
- 2022-04-18 python?如何使用requests下載文件_python
- 2022-12-07 C++中的類成員函數當線程函數_C 語言
- 2022-05-18 opencv?canny邊緣檢測算法詳解_python
- 2022-02-01 for循環中嵌套異步請求導致順序錯亂
- 2022-09-15 python安裝whl文件的實戰步驟_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支