selenium定时签到程序

定时任务

# -*- coding: utf-8 -*-
import time import os
import sched import datetime from com.luoluo.v2ex.mengyou import toQuitAfterTen, toquit, tosign
from com.luoluo.v2ex.randomTime import randomTime, night, morning schedule = sched.scheduler(time.time, time.sleep) def execute_command(cmd,flag, inc):
strftime = time.time()
if cmd < strftime and flag == 0:
toquit()
print('签退')
elif cmd < strftime and flag == 1:
tosign()
print('签到')
else:
print("脚本持续运行中.....")
schedule.enter(inc, 0, execute_command, (cmd,flag, inc)) def main(cmd,flag, inc=60):
# enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,
# 给该触发函数的参数(tuple形式)
schedule.enter(0, 0, execute_command, (cmd,flag, inc))
schedule.run() if __name__ == '__main__':
# 签退
# random_time = '08:41:00'
# random_time = '2017-12-27 ' + random_time
random_time = night()
random_time = time.strftime("%Y-%m-%d ", time.localtime())+random_time
mktime = time.mktime(time.strptime(random_time, '%Y-%m-%d %H:%M:%S'))
print(datetime.datetime.fromtimestamp(mktime))
main(mktime, 0, 900) # 签到
random_time = morning()
random_time = time.strftime("%Y-%m-%d ", time.localtime()) + random_time
# random_time = '10:41:00'
# random_time = '2017-12-27 ' + random_time
mktime = time.mktime(time.strptime(random_time, '%Y-%m-%d %H:%M:%S'))
mktime = mktime + 86400.0
print(datetime.datetime.fromtimestamp(mktime))
main(mktime, 1, 900)

随机产生时间

# -*- coding: utf-8 -*-

import random

def time2seconds(t):
h,m,s = t.strip().split(":")
return int(h) * 3600 + int(m) * 60 + int(s) def seconds2time(sec):
m,s = divmod(sec,60)
h,m = divmod(m,60)
return "%02d:%02d:%02d" % (h,m,s) def randomTime(st,et): sts = time2seconds(st) #sts==27000
ets = time2seconds(et) #ets==34233 return seconds2time(random.randint(sts, ets)) def morning():
st = "08:30:00"
et = "09:00:00"
return randomTime(st, et) def night():
st = "22:00:00"
et = "22:30:00"
return randomTime(st, et) if __name__ == '__main__':
time = randomTime()
print(time)

主程序

# -*- coding: utf-8 -*-
import pytesseract
from PIL import Image,ImageEnhance
import PIL.ImageOps
from selenium import webdriver
from time import sleep
import urllib from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome(executable_path='D:\chromedriver_win32\chromedriver.exe')
driver.get(
"需要被签到的网址") def initTable(threshold=70):
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table def getImage():
image =driver.find_element_by_class_name('yzmImg') driver.save_screenshot('screenshot.png')
left = image.location['x']+3
top = image.location['y']+4
right = image.location['x'] + image.size['width']-3
bottom = image.location['y'] + image.size['height']-4
im = Image.open('screenshot.png')
im = im.crop((left, top, right, bottom))
im.save('screenshot.png') def getCode():
im = Image.open('screenshot.png')
#图片的处理过程
im = im.convert('L')
binaryImage = im.point(initTable(), '1') # im1 = binaryImage.convert('L')
#
# im2 = PIL.ImageOps.invert(im1)
#
# im3 = im2.convert('1')
#
# # out = im3.resize((120,38))
# # im3.show() pytesseract.pytesseract.tesseract_cmd = 'D:\\Users\\soft\\Tesseract-OCR\\tesseract.exe'
asd = pytesseract.image_to_string(binaryImage).replace(" ","")
# print(asd)
if(asd != None and asd !='' and len(asd) ==4):
return asd def toQuitAfterTen():
username = 'username'
password = 'password' getImage()
code = getCode()
while code==None:
image = driver.find_element_by_class_name('yzmImg')
image.click()
getImage()
code = getCode() by_id = driver.find_element_by_id('username')
by_id.clear()
by_id.send_keys(username) element_by_id = driver.find_element_by_id('password')
element_by_id.clear()
element_by_id.send_keys(password) verifyCode = driver.find_element_by_id('verifyCode')
verifyCode.clear()
verifyCode.send_keys(code) driver.find_element_by_css_selector('img[src="/cas/pages/login/hos/login_btn.gif"]').click() def toquit(flags=0):
while flags < 3:
try:
toQuitAfterTen()
sleep(10)
driver.find_element_by_id('outputButton').click()
flags = 99
print("签退成功!")
except:
sleep(4)
flags += 1
print("重试第:"+flags+"次")
sleep(2)
driver.quit()
print(flags) def tosign(flags=0):
while flags < 3:
try:
toQuitAfterTen()
sleep(10)
driver.find_element_by_id('inputButton').click()
flags = 99
print("签到成功!")
except:
sleep(4)
flags += 1
print("重试第: %s次" % flags)
sleep(2)
driver.quit()

最新文章

  1. 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理
  2. javascript设置和获取cookie的通用方法
  3. string.Join()的用法
  4. Loadrunner中参数化实战(8)-Unique+Each occurrence
  5. Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类
  6. Js 一些方法(一)
  7. CDH(Cloudera)与hadoop(apache)对比
  8. MFC Attach()函数和Detach()函数
  9. C语言之函数的介绍
  10. tcp/ip通信传输流
  11. Java面试题复习笔记(Web方向)
  12. css 悬浮框
  13. Windows下Phalcon的安装以及phpstorm识别phalcon语法及提示
  14. javaSE习题 第二章 基本数据类型和数组
  15. Unity --- 纹理为什么要设置为2的N次方
  16. JXL生成Excel,并提供下载(1:生成Excel)
  17. POWER BI报表服务器混合云初了解
  18. Linux gcj命令
  19. docker故障问题修复
  20. 一次JVM调优经历

热门文章

  1. Java学习笔记——Linux下安装配置tomcat
  2. Codeforces Round #564 (Div. 2)A
  3. 分布式个人理解概述和dubbo实现简述
  4. abview查找范例时说 NI服务器未定位 这是怎么回事?
  5. Java学习笔记之---static
  6. WPF 入门笔记之基础
  7. C语言指针专题——为何要学习指针
  8. 版本管理--svn解决代码冲突
  9. 数据库系统概念:基础的SQL
  10. php常用实用函数整理