实时获取淘宝时间
文章源自玩技e族-https://www.playezu.com/797506.html
如今啥东西都搞饥饿营销,啥都要抢,而且抢的人还不少,一旦晚一秒就很难抢到了,本人在经历了好几个月的抢手机抢鞋失败后,终于意识到自己手机的时间跟淘宝不一样!!手机时间比淘宝慢了一秒,这就导致我卡点进去的时候已经比别人慢了,那要如何获得淘宝的实时时间呢,在网上找到了一个写的实时显示淘宝时间的代码,在此分享给大家,有想卡点抢东西的一定要看平台服务器的时间!经过不断摸索,发现有两种方法可以实现。文章源自玩技e族-https://www.playezu.com/797506.html
代码1文章源自玩技e族-https://www.playezu.com/797506.html
文章源自玩技e族-https://www.playezu.com/797506.html
import requests
import time
import tkinter
def funcname():
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'})
x = eval(r1.text)
timeNum = int(x['data']['t'])
timeStamp = float(timeNum/1000)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%H:%M:%S", timeArray)
return otherStyleTime
def tick():
global time1
# 获取时间
time2 = funcname()
# 如果时间发生变化,代码自动更新显示的系统时间
if time2 != time1:
time1 = time2
clock.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
clock.after(200, tick)
win = tkinter.Tk()
win.title("获取淘宝服务器时间")
time1 = ''
clock = tkinter.Label(win, font=('times', 100, 'bold'), bg='green')
clock.grid(row=0, column=0)
tick()
win.mainloop()
文章源自玩技e族-https://www.playezu.com/797506.html
代码2文章源自玩技e族-https://www.playezu.com/797506.html
文章源自玩技e族-https://www.playezu.com/797506.html
import tkinter
import http.client
import time
def get_webservertime(host):
conn = http.client.HTTPConnection(host)
conn.request("GET", "/")
r = conn.getresponse()
ts = r.getheader('date') # 获取http头date部分
# 将GMT时间转换成北京时间
ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)
dat = "%u-u-u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
tm = "u:u:u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
return tm
def tick():
global time1
# 获取时间
time2 = get_webservertime('www.taobao.com')
# 如果时间发生变化,代码自动更新显示的系统时间
if time2 != time1:
time1 = time2
clock.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
clock.after(200, tick)
win = tkinter.Tk()
win.title("获取淘宝服务器时间")
time1 = ''
clock = tkinter.Label(win, font=('times', 100, 'bold'), bg='green')
clock.grid(row=0, column=0)
tick()
win.mainloop()
评论