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实现批量把SVG格式转成png、pdf格式的代码分享
Aug 21 Python
Python常用的日期时间处理方法示例
Feb 08 Python
python学习数据结构实例代码
May 11 Python
详解Python中使用base64模块来处理base64编码的方法
Jul 01 Python
Python数据结构与算法之完全树与最小堆实例
Dec 13 Python
python放大图片和画方格实现算法
Mar 30 Python
pygame实现简易飞机大战
Sep 11 Python
python矩阵的转置和逆转实例
Dec 12 Python
33个Python爬虫项目实战(推荐)
Jul 08 Python
Python 批量刷博客园访问量脚本过程解析
Aug 30 Python
Python实现计算图像RGB均值方式
Jun 04 Python
MAC平台基于Python Appium环境搭建过程图解
Aug 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
php版微信小店调用api示例代码
2016/11/12 PHP
Sample script that deletes a SQL Server database
2007/06/16 Javascript
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
JS实现下拉框的动态添加(附效果)
2013/04/03 Javascript
node.js使用require()函数加载模块
2014/11/26 Javascript
jQuery的层级查找方式分析
2016/06/16 Javascript
jQuery+ajax实现局部刷新的两种方法
2017/06/08 jQuery
javascript 面向对象实战思想分享
2017/09/07 Javascript
记一次vue-webpack项目优化实践详解
2019/02/17 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
[42:20]2014 DOTA2华西杯精英邀请赛5 24 DK VS NewBee
2014/05/25 DOTA
[56:35]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第一局
2016/03/06 DOTA
Python中处理unchecked未捕获异常实例
2015/01/17 Python
python从入门到精通(DAY 2)
2015/12/20 Python
Python正则表达式实现截取成对括号的方法
2017/01/06 Python
python获取程序执行文件路径的方法(推荐)
2018/04/26 Python
python3实现爬取淘宝美食代码分享
2018/09/23 Python
​如何愉快地迁移到 Python 3
2019/04/28 Python
python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能
2019/07/04 Python
python监控进程状态,记录重启时间及进程号的实例
2019/07/15 Python
python3 tcp的粘包现象和解决办法解析
2019/12/09 Python
amazeui时间组件的实现示例
2020/08/18 HTML / CSS
英国花园药房: The Garden Pharmacy
2017/12/28 全球购物
Ooni英国官网:披萨烤箱
2020/05/31 全球购物
经典C++面试题一
2016/11/06 面试题
介绍一下sql server的安全性
2014/08/10 面试题
会计系中文个人求职信
2013/12/24 职场文书
社区工作者感言
2014/03/02 职场文书
项目建议书范文
2014/05/12 职场文书
医德考评自我评价
2014/09/14 职场文书
2014普法依法治理工作总结
2014/12/18 职场文书
护士年终考核评语
2014/12/31 职场文书
2015年七七事变78周年纪念活动方案
2015/05/06 职场文书
公司出差管理制度范本
2015/08/05 职场文书
PL350与SW11的比较
2021/04/22 无线电
MySQL高级进阶sql语句总结大全
2022/03/16 MySQL