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 Deque 模块使用详解
Jul 04 Python
Python使用当前时间、随机数产生一个唯一数字的方法
Sep 18 Python
python、java等哪一门编程语言适合人工智能?
Nov 13 Python
Python cookbook(数据结构与算法)筛选及提取序列中元素的方法
Mar 19 Python
详解用python自制微信机器人,定时发送天气预报
Mar 25 Python
itchat-python搭建微信机器人(附示例)
Jun 11 Python
python求一个字符串的所有排列的实现方法
Feb 04 Python
Python第三方库的几种安装方式(小结)
Apr 03 Python
Windows下Anaconda安装、换源与更新的方法
Apr 17 Python
在python中list作函数形参,防止被实参修改的实现方法
Jun 05 Python
python利用蒙版抠图(使用PIL.Image和cv2)输出透明背景图
Aug 04 Python
对PyTorch中inplace字段的全面理解
May 22 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
用Flash图形化数据(二)
2006/10/09 PHP
PHP验证信用卡卡号是否正确函数
2015/05/27 PHP
PHP获取Exif缩略图的方法
2015/07/13 PHP
PHP判断字符串长度的两种方法很实用
2015/09/22 PHP
PHP+redis实现添加处理投票的方法
2015/11/14 PHP
PHP简单实现二维数组赋值与遍历功能示例
2017/10/19 PHP
Laravel中服务提供者和门面模式的入门介绍
2017/11/06 PHP
用jquery实现的模拟QQ邮箱里的收件人选取及其他效果(一)
2011/01/06 Javascript
jQuery 版元素拖拽原型代码
2011/04/25 Javascript
jquery实现根据浏览器窗口大小自动缩放图片的方法
2015/07/17 Javascript
vue源码学习之Object.defineProperty对象属性监听
2018/05/30 Javascript
微信小程序异步API为Promise简化异步编程的操作方法
2018/08/14 Javascript
vue地址栏直接输入路由无效问题的解决
2018/11/15 Javascript
mpvue+vant app搭建微信小程序的方法步骤
2019/02/11 Javascript
详解vue中的父子传值双向绑定及数据更新问题
2019/06/13 Javascript
node.js实现简单的压缩/解压缩功能示例
2019/11/05 Javascript
python实现问号表达式(?)的方法
2013/11/27 Python
Python中利用sqrt()方法进行平方根计算的教程
2015/05/15 Python
python调用Matplotlib绘制分布点并且添加标签
2018/05/31 Python
用pandas中的DataFrame时选取行或列的方法
2018/07/11 Python
在Python中如何传递任意数量的实参的示例代码
2019/03/21 Python
详解Python下载图片并保存本地的两种方式
2019/05/15 Python
python循环定时中断执行某一段程序的实例
2019/06/29 Python
PyTorch里面的torch.nn.Parameter()详解
2020/01/03 Python
解决paramiko执行命令超时的问题
2020/04/16 Python
python批量处理多DNS多域名的nslookup解析实现
2020/06/28 Python
html5中canvas图表实现柱状图的示例
2017/11/13 HTML / CSS
法国一家多品牌成衣精品中/高档商店:Graduate Store
2019/08/28 全球购物
向全球直邮输送天然健康产品:iHerb.com
2020/05/03 全球购物
遇到的Mysql的面试题
2014/06/29 面试题
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
2014党员学习《反腐倡廉警示教育读本》思想汇报
2014/09/13 职场文书
预备党员转正思想汇报
2014/09/26 职场文书
七夕情人节问候语
2015/11/11 职场文书
各类场合主持词开场白范文集锦
2019/08/16 职场文书
利用前端HTML+CSS+JS开发简单的TODOLIST功能(记事本)
2021/04/13 Javascript