小程序抢票脚本源码调用通用模板
一、背景
之前写了几篇抢场地、抢票的脚本,文章里也贴了源码,但还是有很多朋友没有编程基础,不会将源码串起来调用,所以写了这篇通用的调用模板,供参考。
二、通用模板
import requestsimport timeimport execjsimport random# 源码函数1 #号后面的是注释,起解释说明作用def funtion_1(): #函数具体内容省略 return value_1# 源码函数2,假设有一个参数def funtion_2(arg1): #函数具体内容省略 return value_2# 源码函数3,假设提交抢票请求的函数,假设有两个参数,def funtion_3(arg1, arg2): #函数具体内容省略 return res# 主函数,也就是启动函数,在这里将其他函数串起来,***这是本文重点***def run(): # 调用funtion_1,获取返回值value_1 value_1 = funtion_1() # 调用funtion_2,获取返回值value_2,注意:将value_1传参给funtion_2 value_2 = funtion_2(value_1) # ***以下代码是时间控制器,控制到规定时间才提交抢票请求,否则就一直等待*** round_num = 0 while True: if time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())[-8:] >= start_time: print(\'任务启动时间:\',time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())) break time.sleep(0.1) round_num += 1 if round_num % 600 == 0: print(\'等待中...:\',time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())) # ***以上代码是时间控制器,控制到规定时间才提交抢票请求,否则就一直等待*** # ***以下代码是提交抢票请求,为了提高成功率,我加了控制逻辑*** requests_times = 0 while True: if requests_times >= max_requests_times: print(\'超过自定义最大请求次数,程序退出!\') break try: requests_times += 1 # 调用funtion_3,也就是提交抢票请求,获取返回值res,注意:将value_1和value_2传参给funtion_3,这就叫串起来 res = funtion_3(arg1, arg2) print(time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime()), \'>>>\', res.text) res_json = res.json() if res_json[\"comments\"] == \"成功\": print(\'抢票成功,请及时付款,超时订单自动取消\') break except Exception as e: print(\'请求出错,时间:\', time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime()),e) break # 启动程序之前要准备的参数if __name__ == \'__main__\': start_time = \'12:00:00\' # 放票时间 # access_token每次抢票前两小时修改成最新的,access_token就代表了账号,让别人帮你抢票,只用给这个token就好了 access_token = \"eyJ0eXAiOiJKV1QiLCJjdHkiOiJKV1QiL\" max_requests_times = 3 # 设置最多抢3次,如果抢3次还没抢到说明这次希望不大了 run() # 调用run函数
三、通用模板详解
3.1 导包
2.2 复制源码函数
2.3 写主函数,将源码函数串起来调佣。
我的抢票文章里没有给主函数源码,原因:主函数里的时间调控参数需要自己稍微修改下,跟其他人的请求时间、请求频次岔开一点点,这样能提高成功率。
2.4 程序启动前需要手动修改的参数
四、主函数样例
以付航脱口秀抢票脚本为例,书写主函数,原文链接:小程序付航脱口秀抢票脚本-CSDN博客
def run(): print(\'>>>>>程序已启动>>>>>\') prefilledlist = get_prefilledlist(access_token) preFiledId = prefilledlist[\'preFiledId\'] audienceId = prefilledlist[\'userAudienceIds\'][0] skuId = prefilledlist[\'bizSeatPlanId\'] showId = prefilledlist[\'bizShowId\'] sessionId = prefilledlist[\'bizShowSessionId\'] print(\'>>>>>获取预填信息成功>>>>>preFiledId:%s>>>audienceId:%s>>>skuId:%s>>>showId:%s>>>sessionId:%s>>>\' % (preFiledId,audienceId,skuId,showId,sessionId)) round_num = 0 while True: if time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())[-8:] >= start_time: print(\'任务启动时间:\',time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())) break time.sleep(0.2) round_num += 1 if round_num % 600 == 0: print(\'等待中...:\',time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime())) requests_times = 0 # 整6点生成ticketItems_id ticketItems_id = str(int(time.time()*1000)+random.randint(50, 80)) + \'100000008\' while True: if requests_times >= max_requests_times: print(\'超过自定义最大请求次数,程序退出!\') break try: # 正式发起请求 requests_times += 1 res = get_ticket(ver,bsCityId,locationCityId,preFiledId,audienceId,skuId,showId,sessionId,access_token,ticketItems_id) print(time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime()), \'>>>\', res.text) res_json = res.json() if res_json[\"comments\"] == \"正在为您自动尝试\" or res_json[\"comments\"] == \"该演出还未开售\": time.sleep(1) # 系统默认3秒重复一次请求 elif res_json[\"comments\"] == \"成功\": break except Exception as e: print(\'请求出错,时间:\', time.strftime(\'%Y-%m-%d %H:%M:%S\', time.localtime()),e) breakif __name__ == \'__main__\': start_time = \'18:00:00\' # 抢票开始时间 ver = \"4.20.2\" bsCityId = \"BL1034\" # 默认北京,最好换成自己的 locationCityId = \"1101\" # 默认北京,最好换成自己的 # access_token每次抢票前两小时修改成最新的 access_token = \"your token\" max_requests_times = 3 # 设置最多请求3次,如果请求3次还没抢到说明这次希望不大了 run()
五、模板使用前提
(1)已安装python环境(官网下载python安装,并将python添加到环境变量)
(2)已安装脚本所需要的第三方包(可通过pip install 安装)
(3)其他环境,如node.js(涉及数据加密才会用),Mysql数据库(一般不需要)
往期文章精选:
小程序付航脱口秀抢票脚本-CSDN博客文章浏览阅读3.1k次,点赞38次,收藏21次。付航越火,票越难抢。技术抢票,快乐看猴~_付航脱口秀抢票脚本https://blog.csdn.net/qskylan/article/details/143158517
微信小程序抢场地_小程序抢场地神器-CSDN博客文章浏览阅读1.1k次,点赞14次,收藏19次。sz奥体中心羽毛球抢场地,争取实现打球自由。_小程序抢场地神器https://blog.csdn.net/qskylan/article/details/143021054