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导出DBF文件到Excel的方法
Jul 25 Python
详解Python中 __get__和__getattr__和__getattribute__的区别
Jun 16 Python
Python爬虫代理IP池实现方法
Jan 05 Python
浅谈Scrapy框架普通反爬虫机制的应对策略
Dec 28 Python
Python函数和模块的使用总结
May 20 Python
Python 运行.py文件和交互式运行代码的区别详解
Jul 02 Python
Python3如何判断三角形的类型
Apr 12 Python
Python实现Word表格转成Excel表格的示例代码
Apr 16 Python
Django中Q查询及Q()对象 F查询及F()对象用法
Jul 09 Python
Python3爬虫中Selenium的用法详解
Jul 10 Python
python 实现超级玛丽游戏
Nov 25 Python
用python-webdriver实现自动填表的示例代码
Jan 13 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
合作指挥官:孟斯克
2020/03/16 星际争霸
1 Tube Radio
2021/03/02 无线电
使用eAccelerator加密PHP程序
2008/10/03 PHP
腾讯QQ php程序员面试题目整理
2010/06/08 PHP
PHP strstr 函数判断字符串是否否存在的实例代码
2013/09/28 PHP
Zend Framework教程之Autoloading用法详解
2016/03/08 PHP
php服务器的系统详解
2019/10/12 PHP
些很实用且必用的小脚本代码
2006/06/26 Javascript
兼容多浏览器的iframe自适应高度(ie8 、谷歌浏览器4.0和 firefox3.5.3)
2009/11/04 Javascript
javascript Array.prototype.slice使用说明
2010/10/11 Javascript
js取两个数组的交集|差集|并集|补集|去重示例代码
2013/08/07 Javascript
jquery动态创建div与input的实例代码
2016/10/12 Javascript
JavaScript获取移动设备型号的实现代码(JS获取手机型号和系统)
2018/03/10 Javascript
axios如何利用promise无痛刷新token的实现方法
2019/08/27 Javascript
vue-router重写push方法,解决相同路径跳转报错问题
2020/08/07 Javascript
JavaScript 生成唯一ID的几种方式
2021/02/19 Javascript
python创建线程示例
2014/05/06 Python
Python中的二叉树查找算法模块使用指南
2014/07/04 Python
VScode编写第一个Python程序HelloWorld步骤
2018/04/06 Python
浅谈Python 列表字典赋值的陷阱
2019/01/20 Python
python实现字符串完美拆分split()的方法
2019/07/16 Python
pytorch-RNN进行回归曲线预测方式
2020/01/14 Python
python读取yaml文件后修改写入本地实例
2020/04/27 Python
python中numpy数组与list相互转换实例方法
2021/01/29 Python
Cotton On美国网站:澳洲时装连锁品牌
2016/10/25 全球购物
巴西服装和鞋子购物网站:Marisa
2018/10/25 全球购物
迅雷Cued工作心得体会
2014/01/27 职场文书
小学班级口号
2014/06/09 职场文书
党员对照检查材料
2014/09/22 职场文书
停车位租赁协议书
2014/09/24 职场文书
幼儿园国庆节活动总结
2015/03/23 职场文书
红与黑读书笔记
2015/06/29 职场文书
Oracle11g r2 卸载干净重装的详细教程(亲测有效已重装过)
2021/06/04 Oracle
mybatis源码解读之executor包语句处理功能
2022/02/15 Java/Android
「睡美人」爱洛公主粘土人开订
2022/03/22 日漫
sql server偶发出现死锁的解决方法
2022/04/10 SQL Server