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(
"https://cas.sf-express.com/cas/login?service=http%3A%2F%2Fhos.sf-express.com%2Findex.html&apptiket=dbe979f1b41f6ea2aeb61003de73bf5518cf829000ed010c")
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()
原文地址:https://www.cnblogs.com/luozhiyun/p/8124967.html
时间: 2024-11-05 12:14:56