Centos7 configures py daily automatic punching script from scratch

In the previous blog post, it was mentioned that the automatic clock-in script of python under windows was only written as a local application. This "automation" can only achieve self-starting after booting, which is very inconvenient. How to realize the real "I don't care about you, you can work for me"? Configure the script on the server!

This experiment uses Alibaba Cloud Centos 7.7, and configures an automatic clock-in script from Linux, which has nothing (why is there nothing? Because the previous server was broken for me. Instead of repairing it, it’s better to reinstall it to save trouble. There is nothing funny anyway )

1. Install python3

Install python3 under the server

pip install selenium module

pip3 install selenium

2. Install chrome and 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 test:
google-chrome –version
chromedriver:
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip

Unzip the downloaded file and place it in /usr/bin/chromedriver:

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

Give execution permission:

chmod +x /usr/bin/chromedriver

3. Test whether the installation of the above steps is successful or not

Create file command:

vi test.py

Test code: (After creating the file, press to ienter the insert mode, enter the following in the file)

from selenium import webdriver

Code running:

python3 test.py

Test code:

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. Connect to the code under windows:

Detailed tutorial of script implementation under windows

Compared to the code in the blog above, we need to make some modifications to adapt to the system

Change the driver registration code to:

driver = webdriver.Chrome(chrome_options=chrome_options)

Add in the head:

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')

In addition, I also added a file to input the student ID password, that is, add a config.inifile under the same path of the script , and the content of the file is:

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

Of course, it is also possible to use the above writing to die in the script.

Complete code:

# 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()

I don't know why a warning will be reported. But the python3 command test can run normally.

Add to timed start

First, let's get the absolute path of the script test.py that needs to be started regularly:

find / -name test.py

Get the absolute path: /programs/test.py

Open the timing startup configuration file, a new file may be created the first time you use it.

crontab -e

Briefly introduce the grammar of configuring timing startup:
Insert picture description here
For example, I want my test.py script to be started with python3 at 15:20 every day, then I need to enter:

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

Execute the test script every Tuesday at 9 pm:
0 21 * * 2 python3 /programs/test.py
ESCExit insert mode-"Enter :wqand press Enter to save and exit. carry out!

Guess you like

Origin blog.csdn.net/weixin_44559752/article/details/107645850