Python + selenium + crontab实现每日定时自动打卡功能
作者:Sqwlly 发布时间:2021-06-10 19:45:42
前言
近几日迫于被辅导员三番五次的提醒每日一报打卡,就想着去写个脚本挂在服务器上定时执行。经过我不懈的努力,最终选择了seleniumseleniumselenium,因为简单(
安装selenium库
$ sudo pip install selenium
安装chromdriver
因为我有代理所以直接在官网下载的,那这里你可以选择用淘宝镜像源。
这里为了方便,我直接放命令了。Chromedriver版本我这里选择的是80.0.3987.16(注意要和一会儿下载的Chrome版本一致)。
下载
$ wget https://npm.taobao.org/mirrors/chromedriver/80.0.3987.16/chromedriver_linux64.zip
解压
$ unzip chromedriver_linux64.zip -d .
放到相应目录并授予可执行权限
$ sudo cp chromedriver /usr/bin && sudo chmod +x /usr/bin/chromedriver
安装Chrome安装依赖
$ sudo apt-get install libxss1 libappindicator1 libindicator7
安装Chrome
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb$ sudo dpkg -i google-chrome*.deb$ sudo apt-get install -f
查看版本
$ google-chrome --version
测试调试
$ google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu
编写脚本创建脚本并授予权限
$ touch dailyReport.py && touch dailyReport.log && sudo chmod +x dailyReport.py
内容
# encoding=utf8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
class DailyReport(object):
def __init__(self):
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--no-sandbox') # 这个配置很重要
self.client = None
# self.client = webdriver.Chrome(chrome_options=self.chrome_options)
self.index_url = 'https://xxxxx/xxxx/login'
self.report_url = 'https://xxxx/xxxx/report'
self.data = [
('用户名', '密码'),
('xxxx', 'xxxx'),
('xxxx', 'xxxx'),
('xxxx', 'xxxx'),
('xxxx', 'xxxx')
]
def login(self, _username, _password):
try:
self.client = webdriver.Chrome(chrome_options=self.chrome_options)
print(self.get_current_time() + ' ' + _username + u'开始进行打卡'.encode('utf-8'))
self.client.get(self.index_url)
username = self.client.find_element_by_name("username")
password = self.client.find_element_by_name('password')
username.send_keys(_username)
password.send_keys(_password)
login_button = self.client.find_element_by_xpath('//*[@id="form1"]/div[4]/button')
login_button.click()
except NoSuchElementException:
print(self.get_current_time(), u'登录异常!'.encode('utf-8'))
else:
# time.sleep(2)
print(self.get_current_time() + ' ' + u'登录成功!'.encode('utf-8'))
def post_data(self):
try:
self.client.get(self.report_url)
submit_button = self.client.find_element_by_xpath('//*[@id="p1_ctl00_btnSubmit"]/span/span')
submit_button.click()
ensure_button = self.client.find_element_by_xpath('//*[@id="fineui_26"]/span/span')
ensure_button.click()
# print (client.page_source.encode('utf-8'))
except NoSuchElementException:
print(self.get_current_time(), u' 提交表单异常! 打卡失败!'.encode('utf-8'))
else:
# time.sleep(2)
print(self.get_current_time() + ' ' + u'打卡成功!\n'.encode('utf-8'))
finally:
time.sleep(5)
self.client.quit()
print(u'浏览器退出...\n--------------\n'.encode('utf-8'))
def run(self):
for msg in self.data:
self.login(msg[0], msg[1])
self.post_data()
print('Python script completed at ' + self.get_current_time() + '\n--------------\n')
@staticmethod
def get_current_time():
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if __name__ == '__main__':
obj = DailyReport()
obj.run()
脚本内容需要根据不同网站做对应的修改。
脚本定时执行
这里我们利用LinuxLinuxLinux的内置命令crontabcrontabcrontab,关于crontabcrontabcrontab的用法请自行百度ororor谷歌。
$ crontab -e
如果是首次使用,应该会让你选择编辑器,我选择的vimvimvim,然后在最后一行加入一行
0 0 * * * python ~/dailyReport.py >> ~/dailyReport.log
这样就可以做到每天00:0000:0000:00自动执行脚本了。
注
Chrome在服务器端运行参考博文:https://www.jb51.net/article/183899.htm
来源:https://blog.csdn.net/Eternally831143/article/details/105174883


猜你喜欢
- 一、变量声明的方式let / constlet / const 共同点1.都是块级作用域2.在同一个作用域下,变量名不允许重复3.他们声明的
- 英文文档:classmethod(function)Return a class method for function.A class m
- 近日在项目中遇到一个问题: 如何在报表中统计JSON格式存储的数据?例如有个调查问卷记录表,记录每个问题的答案。 其结构示意如下(横表设计)
- SQL Server中加密是层级的,每一个上层为下提供保护。如图:实例:/** SMK(Service Master Key)在SQL Se
- SMTPSMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP
- 本文实例讲述了SQLSERVER简单创建DBLINK操作远程服务器数据库的方法。分享给大家供大家参考,具体如下:--配置SQLSERVER数
- 背景2010年, OAuth 授权规范 1.0 (rfc 5849) 版本发布, 2年后, 更简单易用的 OAuth 2.0 规范发布(rf
- 前言随着网站的内容的增多和用户访问量的增多,网站加载会越来越慢,受限于带宽和服务器同一时间的请求次数的限制,,我们往往需要在此时对我们的网站
- 在应用系统中,尤其在联机事务处理系统中,对数据查询及处理速度已成为衡量应用系统成败的标准。而采用索引来加快数据处理速度也成为广大数据库用户所
- 笔者需要tensorflow仅运行在一个GPU上(机器本身有多GPU),而且需要依据系统参数动态调节,故无法简单使用CUDA_VISIBLE
- 目录前言初始化项目设计代码实现按需加载播放音频录音长按事件运行调试总结前言相信很多养猫的人都很想跟自己的猫进行沟通,当猫咪发出各种不同声音的
- 我们最终的视图技巧利用了一个高级python技术。 假设你发现自己在各个不同视图里重复了大量代码,就像 这个例子:def my_view1(
- 装饰器模式(Decorator Pattern)是什么装饰器模式是一种结构型模式,它允许你在运行时为一个对象动态地添加新的行为,而不影响其原
- 接着上一篇,统一思想,遵循标准。如何遵循标准,其实标准有很多,结构标准,表现标准,行为标准。选择标准规范,就优先选择W3C推荐的标准。结构标
- python中安装包的方式有很多种:源码包:python setup.py install在线安装:pip install 包名(linux
- 简介在各个语言之中都有时间类型的处理,因为这个地球是圆的(我仿佛在讲废话),有多个时区,每个时区的时间不一样,在程序中有必要存在一种方式,或
- 概述python开发过程中,我们可能需要同时开发多款应用,这些应用可能公用同一个版本的Python程序,但是使用不同版本的第三方库,比如A应
- 前言有时候在使用Python处理比较耗时操作的时候,为了便于观察处理进度,这时候就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了
- 本文实例讲述了mysql外键的三种关系。分享给大家供大家参考,具体如下:因为有foreign key的约束,使得两张表形成了三种了关系:多对
- 很多人都有研究闭合浮动元素的问题,但是解决方法却不一样,也并不是每一种方法都尽善尽美。闭合浮动元素(或者叫清除浮动)是web标准设计中经常会