Python+騰訊云服務(wù)器實(shí)現(xiàn)每日自動(dòng)健康打卡
1.配置需要
python3.7,Chrome或者Edeg瀏覽器,Chrome驅(qū)動(dòng)或者Edge驅(qū)動(dòng)
#需要配置selenium庫(kù),baidu-aip庫(kù),pillOW庫(kù),在終端執(zhí)行以下命令 pip install selenium pip install pillow pip install baidu-aip
2.實(shí)現(xiàn)功能
1.模擬登錄說(shuō)唱大學(xué)微服務(wù),需要百度OCR智能識(shí)別API接口識(shí)別驗(yàn)證碼(免費(fèi)獲?。?/p>
2.虛擬位置信息填寫,注釋:其余信息保留上一天信息
3.反饋打卡信息到QQ郵箱,注釋:需要自行配置POP3/ SMTP服務(wù)
4.掛到騰訊云服務(wù)上,每天定時(shí)自動(dòng)打卡
3.參考鏈接
騰訊云服務(wù)器上運(yùn)行(免費(fèi)1個(gè)月)選擇輕量應(yīng)用服務(wù)器,我選的linux系統(tǒng),這里的實(shí)例也是linux
4.linux服務(wù)器配置
1.以root登錄
2.下載Python3.7,升級(jí)pip,yum,更換國(guó)內(nèi)源
3.安裝庫(kù),執(zhí)行以下命令
pip3 install selenium pip3 install pillow pip3 install baidu-aip
4.在linux上安裝谷歌瀏覽器和驅(qū)動(dòng)
在目錄 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
cd /etc/yum.repos.d/ vim google-chrome.repo
vim命名編輯google-chrome.repo文件,輸入如下內(nèi)容:
[google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch enabled=1 gpgcheck=1 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
具體操作:按i插入,按Esc,然后Shift+;,輸入qw,然后按Enter退出
安裝瀏覽器,依次輸入以下命令
yum -y install google-chrome-stable --nogpgcheck # 檢查版本信息 google-chrome --version # 找到google_chrome路徑:我對(duì)應(yīng)的路徑是/usr/bin/google-chrome,輸入路徑創(chuàng)建軟連接 which google-chrome # 創(chuàng)建軟連接 ln -s /usr/bin/google-chrome /bin/chrome # 安裝驅(qū)動(dòng) wget https://npm.taobao.org/mirrors/chromedriver/88.0.4324.96/chromedriver_linux64.zip # 解壓 yum -y install zip unzip chromedriver_linux64.zip # 轉(zhuǎn)移chromedriver到/user/bin目錄下 sudo mv chromedriver /usr/bin # 解決root運(yùn)行chrome問(wèn)題 vim /opt/google/chrome/google-chrome # 將最后一行改為如下: exec -a "$0" "$HERE/chrome" "$@" --no-sandbox $HOME
將Python自動(dòng)運(yùn)行程序?qū)懙絣inux里:
vim automatic.py # 然后把程序復(fù)制進(jìn)去
測(cè)試運(yùn)行:
python3 automatic.py
利用crontab定時(shí)運(yùn)行python腳本:(輸入如下命令)
crontab -e # 從左到右依次表示分、時(shí)、日、月、周,設(shè)置為每天0:01自動(dòng)打卡 1 0 * * * /usr/bin/python3 /root/automatic.py # 啟動(dòng)服務(wù) service crond restart
5.代碼部分
有詳細(xì)解釋
import sys import time from aip import AipOcr from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from PIL import Image import smtplib from email.mime.text import MIMEText from email.utils import formataddr class LogIn: def __init__(self, user, passwd, path, lat=30.630869, long=104.083748): self.target = 'https://wfw.scu.edu.cn/ncov/wap/default/index' # 說(shuō)唱大學(xué)微服務(wù)地址 self.username = str(user) # 用戶名 self.password = str(passwd) # 密碼 self.lat = lat # 緯度 self.long = long # 經(jīng)度 self.path = path def main(self): attempt = 0 print('\n準(zhǔn)備') chrome_options = webdriver.ChromeOptions() # 設(shè)置無(wú)界面顯示參數(shù),因?yàn)橐旁趌inux服務(wù)器上運(yùn)行,無(wú)法顯示界面,調(diào)試的時(shí)候需要把下面五行注釋掉,顯示chrome界面 chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('window-size=1920x1080') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--hide-scrollbars') chrome_options.add_argument('--headless') s = Service(self.path) browser = webdriver.Chrome(service=s, options=chrome_options)# 加載 chromedriver,用edge的就去下載edgedriver print('開(kāi)始') while True: browser.delete_all_cookies() # 清空cookie browser.get(self.target) try: # 切換為賬號(hào)密碼登錄 browser.switch_to.frame('loginIframe') # 切換frame switch_element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div[2]/div[2]/div[1]/div/div[3]')) ) # 找到對(duì)應(yīng)元素位置 switch_element.click() # 點(diǎn)擊切換 except Exception as error: print('network wrong...\n', error) # 輸入賬號(hào)和密碼 input_user = browser.find_element(by=By.XPATH, value='/html/body/div/div/div[2]/div[2]/div[2]/div[3]/div[1]/div[2]/div/input') input_user.send_keys(self.username) input_pwd = browser.find_element(by=By.XPATH, value='/html/body/div/div/div[2]/div[2]/div[2]/div[3]/div[2]/div[2]/div/input') input_pwd.send_keys(self.password) time.sleep(1) # 截圖驗(yàn)證碼并識(shí)別(這里用的百度云的免費(fèi)OCR),需要自行注冊(cè),不會(huì)的可以參考這篇博客:https://www.cnblogs.com/xiaowenshu/p/11792012.html ver_btn = browser.find_element(by=By.CLASS_NAME, value='van-field__button') ver_btn.click()# 刷新驗(yàn)證碼 # 獲取圖片元素的位置 loc = ver_btn.location # 獲取圖片的寬高 size = ver_btn.size # 獲取驗(yàn)證碼上下左右的位置 left = loc['x'] top = loc['y'] right = (loc['x'] + size['width']) botom = (loc['y'] + size['height']) val = (left, top, right, botom) print(loc) print(size) # 驗(yàn)證碼截圖保存到當(dāng)前目錄下ver.png # 打開(kāi)網(wǎng)頁(yè)截圖 browser.save_screenshot('full.png') # 通過(guò)上下左右的值,去截取驗(yàn)證碼 pic = Image.open('full.png') ver_pic = pic.crop(val) ver_pic.save('ver.png') verification = self.Vertification('ver.png') print('verification code:' + verification) # 識(shí)別驗(yàn)證碼完畢 input_ver = browser.find_element(by=By.XPATH, value='/html/body/div/div/div[2]/div[2]/div[2]/div[3]/div[3]/div[2]/div/input') input_ver.send_keys(verification) browser.find_element(by=By.XPATH, value='/html/body/div/div/div[2]/div[2]/div[2]/div[3]/button').click() # 點(diǎn)擊登錄 time.sleep(5) # 等待跳轉(zhuǎn) if browser.current_url == self.target: break # 登錄成功,退出循環(huán) attempt += 1 if attempt == 5: # 有時(shí)候網(wǎng)頁(yè)會(huì)卡,即使密碼正確也登錄不上去,每次循環(huán)嘗試5次登錄(一般5次內(nèi)能登錄上去) print('請(qǐng)檢查賬號(hào)密碼,或稍后再試!') browser.quit() sys.exit() # 獲取地理位置并提交 browser.execute_cdp_cmd( "Browser.grantPermissions", # 授權(quán)地理位置信息 { "origin": "https://wfw.scu.edu.cn/", "permissions": ["geolocation"] }, ) browser.execute_cdp_cmd( "Emulation.setGeolocationOverride", # 虛擬位置 { "latitude": self.lat, "longitude": self.long, "accuracy": 50, }, ) try: # 提交位置信息 area_element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.NAME, 'area')) ) area_element.click() except Exception as error: print('get location wrong...\n', error) time.sleep(2) # 等待位置信息 """ 郵箱信息,沒(méi)有單獨(dú)寫個(gè)函數(shù),需要配置QQ郵箱,開(kāi)啟POP3/ SMTP服務(wù),并獲取授權(quán)碼 因?yàn)槭翘嵝炎约捍蚩?,所以自己是發(fā)件人,自己是收件人 填入授權(quán)碼 """ # 建立郵箱信息 my_sender = 'XXX@qq.com' # 發(fā)件人郵箱賬號(hào) my_pass = 'XXX' # 發(fā)件人郵箱密碼(當(dāng)時(shí)申請(qǐng)smtp給的口令) my_user = 'XXX@qq.com' # 收件人郵箱賬號(hào) browser.find_element(by=By.XPATH, value='/html/body/div[1]/div/div/section/div[5]/div/a').click() # 提交信息 try: ok_element = WebDriverWait(browser, 3).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div/div[2]/div[2]')) # 提交按鈕 ) ok_element.click() print(self.username, 'success!') WebDriverWait(browser, 3).until( EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div/div[1]')) # 成功對(duì)話框標(biāo)題 ) title_success = browser.find_element(by=By.XPATH, value='/html/body/div[5]/div/div[1]').get_attribute("innerHTML") print('From website:', title_success) msg = MIMEText('打卡成功', 'plain', 'utf-8') msg['From'] = formataddr(["終極打卡人", my_sender]) # 括號(hào)里的對(duì)應(yīng)發(fā)件人郵箱昵稱、發(fā)件人郵箱賬號(hào) msg['To'] = formataddr(["打工人", my_user]) # 括號(hào)里的對(duì)應(yīng)收件人郵箱昵稱、收件人郵箱賬號(hào) msg['Subject'] = "打卡提示" # 郵件的主題,也可以說(shuō)是標(biāo)題 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 發(fā)件人郵箱中的SMTP服務(wù)器,端口是465 server.login(my_sender, my_pass) # 括號(hào)中對(duì)應(yīng)的是發(fā)件人郵箱賬號(hào)、郵箱密碼 server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括號(hào)中對(duì)應(yīng)的是發(fā)件人郵箱賬號(hào)、收件人郵箱賬號(hào)、發(fā)送郵件 server.quit() # 關(guān)閉連接 except: info = browser.find_element(by=By.CLASS_NAME, value='wapat-title').get_attribute('innerHTML') print('From website |', self.username, ':', info) msg = MIMEText('打卡失敗,請(qǐng)手動(dòng)打卡', 'plain', 'utf-8') msg['From'] = formataddr(["終極打卡人", my_sender]) msg['To'] = formataddr(["打工人", my_user]) msg['Subject'] = "打卡提示" server = smtplib.SMTP_SSL("smtp.qq.com", 465) server.login(my_sender, my_pass) server.sendmail(my_sender, [my_user, ], msg.as_string()) server.quit() browser.quit() """ 函數(shù)聲明: 調(diào)用百度OCR的API,需要輸入以下API接口: APP_ID = '***' API_KEY = '***' SECRET_KEY = '***' 傳入截取圖片url,傳出識(shí)別結(jié)果字符串 """ def Vertification(self, url): # 創(chuàng)建AipOcr """ 你的 APPID AK SK """ APP_ID = 'XXX' API_KEY = 'XXX' SECRET_KEY = 'XXX' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 文字識(shí)別高精度版本 """ 讀取圖片 """ def get_file_content(url): with open(url, 'rb') as fp: return fp.read() image = get_file_content('ver.png') """ 調(diào)用通用文字識(shí)別(含位置高精度版) """ result = client.accurate(image) print(str(result)) res = result['words_result'][0]['words'] return str(res) # """ 如果有可選參數(shù) """ # options = {} # options["recognize_granularity"] = "big" # options["detect_direction"] = "true" # options["vertexes_location"] = "true" # options["probability"] = "true" # # """ 帶參數(shù)調(diào)用通用文字識(shí)別(含位置高精度版) """ # client.accurate(image, options) if __name__ == '__main__': """ 用戶輸入?yún)^(qū): 學(xué)號(hào)用戶名 密碼(一般為身份證后六位) 定位地點(diǎn)的經(jīng)緯度 """ username = 'XXX' # 用戶名(學(xué)號(hào)) password = 'XXX' # 密碼 latitude = 30.630869 # 虛擬位置緯度 longitude = 104.083748 # 經(jīng)度 path = '.\chromedriver\chromedriver.exe' #chromedriver路徑 # path = '/usr/bin/chromedriver' # linux服務(wù)器上的chromedriver路徑 t = LogIn(user=username, passwd=password, lat=latitude, long=longitude, path=path) t.main()
6.運(yùn)行結(jié)果圖
到此這篇關(guān)于Python+騰訊云服務(wù)器實(shí)現(xiàn)每日自動(dòng)健康打卡的文章就介紹到這了,更多相關(guān)Python 自動(dòng)健康打卡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python協(xié)程的四種實(shí)現(xiàn)方式總結(jié)
今天繼續(xù)給大家介紹Python關(guān)知識(shí),本文主要內(nèi)容是Python協(xié)程的四種實(shí)現(xiàn)方式。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-01-01django的安裝和創(chuàng)建應(yīng)用過(guò)程詳解
這篇文章主要介紹了django的安裝和創(chuàng)建應(yīng)用,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07PHP基于phpqrcode類庫(kù)生成二維碼過(guò)程解析
這篇文章主要介紹了PHP基于phpqrcode類庫(kù)生成二維碼過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python pass語(yǔ)句作用和Python assert斷言函數(shù)的用法
這篇文章主要介紹了Python pass語(yǔ)句作用和Python assert斷言函數(shù)的用法,文章內(nèi)容介紹詳細(xì)具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-03-03Python實(shí)現(xiàn)備份文件實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)備份文件的方法,可實(shí)現(xiàn)針對(duì)各類常見(jiàn)擴(kuò)展名的文件進(jìn)行備份的功能,需要的朋友可以參考下2014-09-09