#-*-coding:GBK -*-
import os
import winreg #操作注册表
import re
import sys
from tkinter import *
from win32gui import *
import tkinter as tk
import tkinter.messagebox as msg
import win32gui,win32api,win32con
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
import requests
from datetime import datetime
import threading #多线程
import configparser
from datetime import datetime, timedelta
import ctypes #隐藏控制台库
import shelve# 保存变量
def hideConsole(): #隐藏控制台
"""
Hides the console window in GUI mode. Necessary for frozen application, because
this application support both, command line processing AND GUI mode and theirfor
cannot be run via pythonw.exe.
"""
whnd = ctypes.windll.kernel32.GetConsoleWindow()
if whnd != 0:
ctypes.windll.user32.ShowWindow(whnd, 0)
# if you wanted to close the handles...
#ctypes.windll.kernel32.CloseHandle(whnd)
hideConsole()#隐藏控制台
def key360(): #获取360浏览器位置
UnInsKey360 = '360SeSES\shell\open\command'
key360 = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, UnInsKey360)
name,value,type = winreg.EnumValue(key360,0) #注册表键名,键值,数据类型
num = re.findall(r"(.+?)360se.exe",value)
num = num[0]+'360se.exe'
return(num)
def test(content): #输入框内容限定为数字
# 如果不加上==""的话,就会发现删不完。总会剩下一个数字
if content.isdigit() or content == "":
return True
else:
return False
def thread_it(func, *args):
'''将函数打包进线程'''
# 创建
t = threading.Thread(target=func, args=args)
# 守护 !!!
t.setDaemon(True)
# 启动
t.start()
# 阻塞--卡死界面!
# t.join()
def time_server():
# 获取淘宝服务器的时间戳
r1 = requests.get(url='http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36'}
).json()['data']['t']
# 把时间戳格式/1000 获取毫秒
timeNum = int(r1) / 1000
# 格式化时间 (小数点后6为)
time1 = datetime.fromtimestamp(timeNum)
return time1
def time_server1():
# 获取淘宝服务器的时间戳
r1 = requests.get(url='http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36'}
).json()['data']['t']
# 把时间戳格式/1000 获取毫秒
timeNum = int(r1) / 1000
# 格式化时间 (小数点后6为)
time5 = datetime.fromtimestamp(timeNum)
return time5
def ping():
ip = 'cart.taobao.com'
backinfo =os.popen('ping -n 1 -w 1 %s'%ip).readlines()[7]
return(backinfo)
global backinfo1
backinfo1= ping()
time2 = time_server().strftime("%Y")
time3 = time_server().strftime("%m")
time4 = time_server().strftime("%d")
class AlarmClock(Tk):
def __init__(self):
super().__init__()
self.title('淘宝购物车秒杀器')
#self.minsize(width=600, height=300)
self.resizable(0, 0)
# 第3步,设定窗口的大小(长 * 宽)
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
dialog_width = 600
dialog_height = 480
self.geometry("%dx%d+%d+%d" % (dialog_width, dialog_height, (screenwidth-dialog_width)/2, (screenheight-dialog_height)/2)) # 这里的乘是小x
# 设置控件
Label(self, text='现在时间(淘宝时间戳):', font=('Arial', 15)).place(x=10, y=10)
def refresher():
self.now_time = time_server() #获取淘宝时间
self.var_nowtime = StringVar(value=self.now_time)
Label(self, textvariable=self.var_nowtime, font=('Arial', 15), bg='#D6EAF8').place(x=50, y=50)
self.after(1000, refresher)
#threading.Timer(1, refresher).start()
refresher()
Label(self, text='计算延迟:', font=('Arial', 15)).place(x=10, y=85)
self.backinfo2 = StringVar(value=backinfo1)
Label(self, textvariable=self.backinfo2, font=('宋体', 15), bg='#D6EAF8',justify='center').place(x=50, y=125)
Label(self, text='设置定时:', font=('Arial', 15)).place(x=10, y=200)
Label(self, text='时', font=('Arial', 15)).place(x=285, y=200)
Label(self, text='分', font=('Arial', 15)).place(x=345, y=200)
Label(self, text='秒', font=('Arial', 15)).place(x=405, y=200)
Label(self, text='毫秒', font=('Arial', 15)).place(x=465, y=200)
shelffile = shelve.open('mydata')
self.var_year = StringVar(value=time2)
self.var_month = StringVar(value=time3)
self.var_day = StringVar(value=time4)
self.var_hour = StringVar(value=shelffile['timesave1'][0])
self.var_min = StringVar(value=shelffile['timesave1'][1])
self.var_sec = StringVar(value=shelffile['timesave1'][2])
self.var_mil_sec = StringVar(value=shelffile['timesave1'][3])
self.entry_year = Entry(self, textvariable=self.var_year, font=('Arial', 15), bg='#D6EAF8', width=6,
justify='center')
self.entry_year.place(x=60, y=240)
Label(self, text='-', font=('Arial', 15)).place(x=125, y=240)
self.entry_month = Entry(self, textvariable=self.var_month, font=('Arial', 15), bg='#D6EAF8', width=3,
justify='center')
self.entry_month.place(x=140, y=240)
Label(self, text='-', font=('Arial', 15)).place(x=185, y=240)
self.entry_day = Entry(self, textvariable=self.var_day, font=('Arial', 15), bg='#D6EAF8', width=3,
justify='center')
self.entry_day.place(x=200, y=240)
self.entry_hour = Entry(self, textvariable=self.var_hour, font=('Arial', 15), bg='#D6EAF8', width=3,
justify='center')
self.entry_hour.place(x=280, y=240)
Label(self, text=':', font=('Arial', 15)).place(x=325, y=240)
self.entry_min = Entry(self, textvariable=self.var_min, font=('Arial', 15), bg='#D6EAF8', width=3,
justify='center')
self.entry_min.place(x=340, y=240)
Label(self, text=':', font=('Arial', 15)).place(x=385, y=240)
self.entry_sec = Entry(self, textvariable=self.var_sec, font=('Arial', 15), bg='#D6EAF8', width=3,
justify='center')
self.entry_sec.place(x=400, y=240)
Label(self, text='.', font=('Arial', 15)).place(x=445, y=240)
self.entry_mil_sec = Entry(self, textvariable=self.var_mil_sec, font=('Arial', 15), bg='#D6EAF8', width=7,
justify='center')
self.entry_mil_sec.place(x=460, y=240)
# 因为只能设定一个闹钟,所以再次设定的话要重置
self.btn_replace = Button(self, text='重置并保存', font=('宋体', 12), width=12, command=lambda :thread_it(self.replace))
self.btn_replace.place(x=180, y=290)
# 开关按钮控制闹钟的开和关,初始是OFF状态,点击变成ON状态启动闹钟
self.btn_begin = Button(self, text='开始秒杀', font=('宋体', 12), width=10, command=lambda :thread_it(self.beginning))
self.btn_begin.place(x=300, y=290)
self.mainloop()
def beginning(self):
flag = self.btn_begin['text'] # 定义一个flag来表示闹钟的状态
# 如果是打开状态就什么也不做
if flag == '秒杀中……':
return
# 如果是关闭状态,就把text变为ON,三个输入框变为不可编辑
else:
self.btn_begin['text'] = '秒杀中……'
self.entry_year['state'] = DISABLED
self.entry_month['state'] = DISABLED
self.entry_day['state'] = DISABLED
self.entry_hour['state'] = DISABLED
self.entry_min['state'] = DISABLED
self.entry_sec['state'] = DISABLED
self.entry_mil_sec['state'] = DISABLED
self.btn_begin['state'] = DISABLED
__browser_url = key360() ##360浏览器的地址 ##如果启用的是360极速浏览器,改360se.exe为360Chrome.exe(原因你懂得),前面是路径,这个你也懂得。
chrome_options = Options()
chrome_options.binary_location = __browser_url
path1=os.getcwd()
path =os.path.join(path1,"78.0.39.4.108\chromedriver.exe") # 谷歌chromedriver完整路径
# 输入秒杀时间
start_time = self.entry_year.get() + '-' + self.entry_month.get() +'-'+ self.entry_day.get() +' ' + self.entry_hour.get() +':' + self.entry_min.get() +':'+self.entry_sec.get()+ '.'+self.entry_mil_sec.get()
#password = input("输入付款密码:")
msg.showinfo(title='登录', message='只有60秒的登录时间')
# 将输入的时间进行格式化
timeArray = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S.%f")
# 用来判断 你是订单提交失败还是支付失败
sum = 0
# 设置chrome驱动的路径
#driver = webdriver.Chrome(executable_path=path)
options=chrome_options
global driver
driver = webdriver.Chrome(path,options=chrome_options)
#options=webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
# 打开淘宝的登录界面
driver.get("https://cart.taobao.com/cart.htm")
# 最大化浏览器
driver.maximize_window()
# 判断全选框是否出现 出现则点击全选 否则继续等待 最多等待60秒
try:
WebDriverWait(driver, 60, 0.001).until(
lambda el: driver.find_element_by_xpath('//*[@id="J_SelectAll1"]/div/label')).click()
except:
msg.showinfo(title='错误', message='登录失败')
#msg.showinfo(title='等待', message='等待中')
while True:
# 判断时间服务器时间是否大于或等于输入的时间
if time_server1() >= timeArray:
# 点击结算
#print(time_server())
driver.find_element_by_xpath('//*[@id="J_Go"]').click()
#print(time_server())
break
else:
continue
try:
# 判断提交订单的按钮是否出现 出现就点击 否则继续等待 最多等待3秒
WebDriverWait(driver, 3, 0.000).until(
lambda el: driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]')).click()
msg.showinfo(title='成功', message='订单提交成功')
sum = 1
# 判断输入密码的框是否出现 出现就输入密码
WebDriverWait(driver, 50, 0.001).until(
lambda el: driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]')).send_keys(password)
# 点击确认付款
driver.find_element_by_xpath('//*[@id="J_authSubmit"]').click()
except:
if sum == 0:
msg.showinfo(title='失败', message='提交订单失败')
else:
msg.showinfo(title='失败', message='提交订单失败')
def replace(self):
# 重置按钮把三个输入框内容变为00,同时变为可编辑状态,开关按钮变为OFF
self.btn_begin['text'] = '开始秒杀'
self.entry_year['state'] = NORMAL
self.entry_month['state'] = NORMAL
self.entry_day['state'] = NORMAL
self.entry_hour['state'] = NORMAL
self.entry_min['state'] = NORMAL
self.entry_sec['state'] = NORMAL
self.entry_mil_sec['state'] = NORMAL
self.btn_begin['state'] = NORMAL
shelffile = shelve.open('mydata')
timesave = [self.entry_hour.get(),self.entry_min.get(),self.entry_sec.get(),self.entry_mil_sec.get()]
shelffile['timesave1'] = timesave
shelffile.close()
driver.quit()
msg.showinfo(title='重置成功', message='重置成功')
my_clock = AlarmClock()
淘宝购物车秒杀器(免费版)
链接:https://pan.baidu.com/s/11h2LRATrcUnArmCN_UFKqA
提取码:7ixm