浮生半日闲 | 服务器自动打卡脚本

前言

Buaaer肯定看出来为啥要写这个了hh

做的比较晚,因为之前一直没想上手py(

这几天工作差不多无了就搞一搞

实现过程曲折不少,毕竟全新知识,记录一哈

KeyWord:

  • py selenium里webdriver方法

  • chrome driver

  • linux 终端操作

  • cron 定时脚本

  • 邮件提醒

知识储备:

  • 前端知识(html元素、js方法调用及调试)

  • linux基础

  • py

Step1 在linux上准备chrome&chrome-drive&py开发环境

参考https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/

基础配置不再啰嗦

几个tips:

1.chrome 和 ~driver版本号要一致

2.chrome driver 可以用淘宝的镜像源

3.一堆依赖库注意安装

Step2 自动打卡模块

dailyRoport.py:


#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from time import sleep

user = ""#账号
passwd = "!!!"#密码
longitude = "39.984314"
latitude = "116.345935"#经纬度我给集成进去了
log = open('rua.log', 'a')
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
url = "https://app.buaa.edu.cn/uc/wap/login?redirect=https%3A%2F%2Fapp.buaa.edu.cn%2Fsite%2Fncov%2Fxisudailyup"
header = {
   'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
browser.get(url)
print(browser.title)
browser.implicitly_wait(3)
username = browser.find_element_by_tag_name("input")
username.send_keys(user)
password = browser.find_element_by_xpath("//input[@type='password']")
password.send_keys(passwd)
login_button = browser.find_element_by_css_selector('div.btn')
browser.execute_script("$(arguments[0]).click()", login_button)
try:
   element = WebDriverWait(browser, 10).until(
       EC.presence_of_element_located((By.NAME, "qtqk")))
finally:
   print(browser.title)
browser.execute_script("window.navigator.geolocation.getCurrentPosition=function(success,options){var position = {coords:{ latitude:39.984105 ,longitude:116.345756 }};success(position);var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0};}")
browser.save_screenshot('/root/screenshot/area.png')
tiwen = browser.find_element_by_xpath("//div[@name='tw']/div/div[2]")
tiwen.click()
zfzx = browser.find_element_by_xpath("//div[@name='sfzx']/div/div[1]")
zfzx.click()
where = browser.find_element_by_name('area')
where.click()
sleep(3)
browser.save_screenshot('/root/screenshot/geo.png')
suuubmit = browser.find_element_by_class_name('footers')
suuubmit.click()
browser.save_screenshot('/root/screenshot/confirm.png')
sleep(1)
ActionChains(browser).move_by_offset(360, 640).click().perform()
browser.save_screenshot('ok.png')
datee = datetime.date.today()

print("%s 成功打卡!" % (datee), file=log)
print("%s 成功打卡!" % (datee))
sleep(50)
browser.quit()
log.close()


整体代码参考了https://github.com/colasama/buaa-ncov-hitcarder,有兴趣的可以去学习下

简要记录一些交互方法:

  • https://selenium-python-zh.readthedocs.io/en/latest/locating-elements.html

    seleniun官方文档

  • browser.execute_script("window.navigator.geolocation.getCurrentPosition=function(success,options){var position = {coords:{ latitude:39.984105 ,longitude:116.345756 }};success(position);var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0};}")

    调用js方法,这里是将获得当前经纬度的函数修改了其中变量,要不然我的服务器在哪儿我自己都不知道,甚至可能被阿里云禁止获得当前地址,学校的定位方法(瞅了瞅js,用的高德地图的api)更获取不了了

Step3 设置定时

1.创立一个sh脚本daka.sh

#!/iin/sh
cd /root
./dailyReport.py
echo "done"

2.创立一个cron进程

11 18 * * * sh daka.sh >> daka.log

3.测试

sudo cron -l

看看有无当前脚本

Step4 邮件系统

mail.py

#! /usr/bin/env python
#coding=utf-8
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL

host_server = 'smtp.qq.com'
sender_qq = '1454034216'
pwd = 'zj###########dgc'
sender_qq_mail = '1454034216@qq.com'
receiver = '1454034216@qq.com'
mail_content = '今日第一封邮件,没收到第二封就乖乖去打卡'
mail_title = '打卡1'


smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)

msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())


do()#本地的文件,所以没写,懒得从服务器上摘了


mail_content = 'success'
mail_title = '打卡成功'

msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()

这样理想情况是每天6点11收到打卡成功的邮件~

感觉写出来没啥东西,但是当时做的时候确实看了很多资料w

只能说这种应用型的小作品 知难行易吧


请叫我zz 这里分享生活里的一切 就当作我昂贵的日记本吧