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使用xlrd读取Excel格式文件的方法
Mar 10 Python
python使用分治法实现求解最大值的方法
May 12 Python
Python数据类型中的“冒号“[::]——分片与步长操作示例
Jan 24 Python
django celery redis使用具体实践
Apr 08 Python
python 接口实现 供第三方调用的例子
Aug 13 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
Feb 15 Python
Pycharm修改python路径过程图解
May 22 Python
Python 实现自动登录+点击+滑动验证功能
Jun 10 Python
python中selenium库的基本使用详解
Jul 31 Python
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
Sep 28 Python
Python实现GIF动图以及视频卡通化详解
Dec 06 Python
Python中文分词库jieba(结巴分词)详细使用介绍
Apr 07 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
为了这两部电子管收音机,买了6套全新电子管和10粒刻度盘灯泡
2021/03/02 无线电
PHP禁止个别IP访问网站
2013/10/30 PHP
php中删除、清空session的方式总结
2015/10/09 PHP
thinkphp在php7环境下提示Cannot use ‘String’ as class name as it is reserved的解决方法
2016/09/30 PHP
PHP正则验证字符串是否为数字的两种方法并附常用正则
2019/02/27 PHP
JQuery autocomplete 使用手册
2010/04/01 Javascript
基于jQuery的可以控制左右滚动及自动滚动效果的代码
2010/07/25 Javascript
jQuery选择器中含有空格的使用示例及注意事项
2013/08/25 Javascript
JS:window.onload的使用介绍
2013/11/13 Javascript
js/jQuery简单实现选项卡功能
2014/01/02 Javascript
jQuery菜单插件superfish使用指南
2015/04/21 Javascript
jQuery实现带分组数据的Table表头排序实例分析
2015/11/24 Javascript
基于javascript实现右下角浮动广告效果
2016/01/08 Javascript
gulp加批处理(.bat)实现ng多应用一键自动化构建
2017/02/16 Javascript
详解使用fetch发送post请求时的参数处理
2017/04/05 Javascript
监听angularJs列表数据是否渲染完毕的方法示例
2018/11/07 Javascript
vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)
2019/04/12 Javascript
Vue使用虚拟dom进行渲染view的方法
2019/12/26 Javascript
Vue引入Stylus知识点总结
2020/01/16 Javascript
jQuery实现放大镜案例
2020/10/19 jQuery
微信小程序 接入腾讯地图的两种写法
2021/01/12 Javascript
[19:24]DOTA2客户端使用指南 一分钟快速设置轻松超神
2013/09/24 DOTA
Python中的条件判断语句与循环语句用法小结
2016/03/21 Python
Python 搭建Web站点之Web服务器与Web框架
2016/11/06 Python
对python指数、幂数拟合curve_fit详解
2018/12/29 Python
关于python的缩进规则的知识点详解
2020/06/22 Python
python中的垃圾回收(GC)机制
2020/09/21 Python
python 基于Apscheduler实现定时任务
2020/12/15 Python
教师新年寄语
2014/04/03 职场文书
委托书格式范文
2015/01/28 职场文书
2015年七七事变78周年纪念活动方案
2015/05/06 职场文书
2016年“世界气象日”广播稿
2015/12/17 职场文书
小学运动会入场口号
2015/12/24 职场文书
mysql 如何获取两个集合的交集/差集/并集
2021/06/08 MySQL
试了下Golang实现try catch的方法
2021/07/01 Golang
pd.DataFrame中的几种索引变换的实现
2022/06/16 Python