Python + selenium + crontab实现每日定时自动打卡功能


Posted in Python onMarch 31, 2020

前言

近几日迫于被辅导员三番五次的提醒每日一报打卡,就想着去写个脚本挂在服务器上定时执行。经过我不懈的努力,最终选择了seleniumseleniumselenium,因为简单(

安装selenium库

$ sudo pip install selenium

安装chromdriver

因为我有代理所以直接在官网下载的,那这里你可以选择用淘宝镜像源。

Python + selenium + crontab实现每日定时自动打卡功能

这里为了方便,我直接放命令了。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://3water.com/article/183899.htm

到此这篇关于Python + selenium + crontab实现每日定时自动打卡的文章就介绍到这了,更多相关python 定时自动打卡内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python类继承用法实例分析
May 27 Python
Python装饰器使用实例:验证参数合法性
Jun 24 Python
Python采用Django制作简易的知乎日报API
Aug 03 Python
django项目运行因中文而乱码报错的几种情况解决
Nov 07 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
Dec 21 Python
详解用python生成随机数的几种方法
Aug 04 Python
python 视频逐帧保存为图片的完整实例
Dec 10 Python
PyTorch中 tensor.detach() 和 tensor.data 的区别详解
Jan 06 Python
python实现五子棋程序
Apr 24 Python
Python Selenium实现无可视化界面过程解析
Aug 25 Python
python中的测试框架
Nov 13 Python
解决python3输入的坑——input()
Dec 05 Python
python实现udp聊天窗口
Mar 31 #Python
浅谈在django中使用filter()(即对QuerySet操作)时踩的坑
Mar 31 #Python
Python sorted排序方法如何实现
Mar 31 #Python
解决Django中checkbox复选框的传值问题
Mar 31 #Python
Python文本文件的合并操作方法代码实例
Mar 31 #Python
Python调用接口合并Excel表代码实例
Mar 31 #Python
Python如何批量获取文件夹的大小并保存
Mar 31 #Python
You might like
php列出一个目录下的所有文件的代码
2012/10/09 PHP
php小技巧之过滤ascii控制字符
2014/05/14 PHP
php实现文本数据导入SQL SERVER
2015/05/17 PHP
php获取远程文件大小
2015/10/20 PHP
List the Codec Files on a Computer
2007/06/18 Javascript
fix-ie5.js扩展在IE5下不能使用的几个方法
2007/08/20 Javascript
jquery ajax提交表单数据的两种方式
2009/11/24 Javascript
读jQuery之一(对象的组成)
2011/06/11 Javascript
jquery 插件学习(二)
2012/08/06 Javascript
javascript 判断整数方法分享
2014/12/16 Javascript
JS函数的定义与调用方法推荐
2016/05/12 Javascript
关于json字符串与实体之间的严格验证代码
2016/11/10 Javascript
jquery实现弹窗功能(窗口居中显示)
2017/02/27 Javascript
浅谈AngularJs 双向绑定原理(数据绑定机制)
2017/12/07 Javascript
JavaScrip关于创建常量的知识点
2017/12/07 Javascript
node使用promise替代回调函数
2018/05/07 Javascript
three.js利用射线Raycaster进行碰撞检测
2020/03/12 Javascript
安装多版本Vue-CLI的实现方法
2020/03/24 Javascript
使用Webpack 搭建 Vue3 开发环境过程详解
2020/07/28 Javascript
Python批量修改文件后缀的方法
2014/01/26 Python
python中map的基本用法示例
2018/09/10 Python
解决python3捕获cx_oracle抛出的异常错误问题
2018/10/18 Python
使用python3实现操作串口详解
2019/01/01 Python
python 中的列表生成式、生成器表达式、模块导入
2019/06/19 Python
PyTorch中常用的激活函数的方法示例
2019/08/20 Python
Keras—embedding嵌入层的用法详解
2020/06/10 Python
利用CSS3实现圆角的outline效果的教程
2015/06/05 HTML / CSS
美国著名珠宝品牌之一:Jared The Galleria Of Jewelry
2016/10/01 全球购物
劳资员岗位职责
2013/11/11 职场文书
幼儿园保教管理制度
2014/02/03 职场文书
《童趣》教学反思
2014/02/19 职场文书
领导接待方案
2014/03/13 职场文书
2014年教师节演讲稿范文
2014/09/10 职场文书
建筑工地资料员岗位职责
2015/04/13 职场文书
刑事案件上诉状
2015/05/23 职场文书
导游词之南京栖霞山
2019/10/18 职场文书