Centos7从零开始配置py每日自动打卡脚本

上一篇博文说到windows下python的自动打卡脚本,仅仅写成了一个本地应用,这个“自动化”最多只能达到开机自启动 也很不方便啊。怎么实现真正的“我不用管你你也能给我干活”呢?把脚本配置上服务器吧!

本次实验使用的是阿里云Centos7.7,从啥也没有的linux开始配置一个自动打卡脚本(为啥是啥也没有?因为之前那个服务器给我整坏了。与其修复还不如干脆重装省事 反正本来也没啥东西滑稽

1. 安装python3

服务器下安装python3

pip安装selenium模块

pip3 install selenium

2. 安装chrome和chromedriver

chrome:

curl https://intoli.com/install-google-chrome.sh | bash
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install ./google-chrome-stable_current_*.rpm
chrome测试:
google-chrome –version
chromedriver:
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip

将下载的文件解压,放在/usr/bin/chromedriver:

unzip chromedriver_linux64.zip -d /usr/bin/chromedriver

给予执行权限:

chmod +x /usr/bin/chromedriver

3.上述步骤安装成功与否的测试

创建文件命令:

vi test.py

测试代码:(创建文件以后按i进入insert模式,文件中输入以下)

from selenium import webdriver

代码运行:

python3 test.py

测试代码:

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from configparser import ConfigParser

from pyrsistent._transformations import inc

# 只需修改下面三项即可 学号 密码 收件箱

url = "http://tjxx.lnu.edu.cn/"

movement = "/html/body/div[1]/form/div[3]/div[2]/label[1]"#有无移动
touch = "/html/body/div[1]/form/div[4]/div[2]/label[1]"#有无接触史
condition = "/html/body/div[1]/form/div[5]/div[2]/label[1]"#健康状况
quarantine = "/html/body/div[1]/form/div[6]/div[2]/label[1]"#是否隔离
inCollege = "/html/body/div[1]/form/div[7]/div[2]/label[1]"#是否在校
submit = "/html/body/div[1]/form/div[8]/a"#提交
check = "/html/body/div[3]/div[2]/div[2]/a[2]"#最后的确定按钮

# 模拟登陆打卡
def do_login(driver,userName, password):
    # driver.maximize_window() 将窗口最大化
    # 找到登录框 输入账号密码
    driver.find_element_by_name('userid').send_keys(userName)
    driver.find_element_by_name('userpwd').send_keys(password)
    driver.find_element_by_id('formSubmitBtn').click()#点击登录

    driver.find_element_by_xpath(movement).click()
    driver.find_element_by_xpath(touch).click()
    driver.find_element_by_xpath(condition).click()
    driver.find_element_by_xpath(quarantine).click()
    driver.find_element_by_xpath(inCollege).click()
    
    driver.find_element_by_xpath(submit).click()#点击提交
    time.sleep(2)
    driver.find_element_by_xpath(check).click()#点击确定
    time.sleep(1)

if __name__ == '__main__':
    # 模拟浏览器打开网站
    cf = ConfigParser()
    cf.read("config.ini")   
    userName = cf.get("config", "userName")
    password = cf.get("config", "password")
    driver = webdriver.Chrome()
    driver.get(url)
    # 登录并打卡
    do_login(driver,userName, password)
#     sendMail(nowTime + "\n打卡成功!")
    print("打卡结束")
    time.sleep(1)
    driver.quit()

4.接轨windows下的代码:

windows下的脚本实现详细教程

相比于上面这篇博客的代码,我们需要做一些修改以适配系统

更改驱动注册代码,改为:

driver = webdriver.Chrome(chrome_options=chrome_options)

在头部添加:

import time
from selenium.webdriver.chrome.options import Options
# guarantee our program can be in operation under command line
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--disable-gpu')

另外我还添加了一个文件输入学号密码的操作,即在脚本同一路径下添加一个config.ini文件,文件内容为:

[config]
userName = 登陆用户名
password = 密码

当然用上文的写死在脚本里也是可以的。

完整代码:

# encoding: utf-8
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from configparser import ConfigParser

from pyrsistent._transformations import inc

# guarantee our program can be in operation under command line
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--disable-gpu')

# 只需修改下面三项即可 学号 密码 收件箱

url = "打卡网站"

# 每次需要填写的位置的XPath:xpath的获取见上一篇博文 “windows下的脚本实现详细教程”
movement = "/html/body/div[1]/form/div[3]/div[2]/label[1]"#有无移动
touch = "/html/body/div[1]/form/div[4]/div[2]/label[1]"#有无接触史
condition = "/html/body/div[1]/form/div[5]/div[2]/label[1]"#健康状况
quarantine = "/html/body/div[1]/form/div[6]/div[2]/label[1]"#是否隔离
inCollege = "/html/body/div[1]/form/div[7]/div[2]/label[1]"#是否在校
submit = "/html/body/div[1]/form/div[8]/a"#提交
check = "/html/body/div[3]/div[2]/div[2]/a[2]"#最后的确定按钮

# 模拟登陆打卡
def do_login(driver,userName, password):
    # driver.maximize_window() 将窗口最大化
    # 找到登录框 输入账号密码
    driver.find_element_by_name('userid').send_keys(userName)
    driver.find_element_by_name('userpwd').send_keys(password)
    driver.find_element_by_id('formSubmitBtn').click()#点击登录

    driver.find_element_by_xpath(movement).click()
    driver.find_element_by_xpath(touch).click()
    driver.find_element_by_xpath(condition).click()
    driver.find_element_by_xpath(quarantine).click()
    driver.find_element_by_xpath(inCollege).click()
    
    driver.find_element_by_xpath(submit).click()#点击提交
    time.sleep(2)
    driver.find_element_by_xpath(check).click()#点击确定
    time.sleep(1)

if __name__ == '__main__':
    # 模拟浏览器打开网站
    cf = ConfigParser()
    cf.read("config.ini")   
    userName = cf.get("config", "userName")
    password = cf.get("config", "password")
    driver = webdriver.Chrome(chrome_options=chrome_options)# Optional argument, if not specified will search path.
    driver.get(url)
    # 登录并打卡
    do_login(driver,userName, password)
#     sendMail(nowTime + "\n打卡成功!")
    print("打卡结束")
    time.sleep(1)
    driver.quit()

不知道什么原因会报警告。但是python3命令测试可以正常运行了。

添加到定时启动

首先我们获取一下需要定时启动的脚本test.py的绝对路径:

find / -name test.py

得到绝对路径:/programs/test.py

打开定时启动配置文件,第一次使用可能会新建一个文件。

crontab -e

简单介绍一下配置定时启动的文法:
在这里插入图片描述
举个例子,我希望我的test.py脚本每天15:20用python3启动,那么我需要输入:

20 15 * * * python3 /programs/test.py

每周二晚上9点执行test脚本:
0 21 * * 2 python3 /programs/test.py
ESC退出插入模式-》输入:wq以后回车保存退出。完成!

猜你喜欢

转载自blog.csdn.net/weixin_44559752/article/details/107645850
今日推荐