周杰伦演唱会总是抢不到票?教你用Python做一个自动抢票脚本!

相信想去周董演唱会的大家都用过大麦网抢票吧?

可是

抢不到啊

该说不说 我抢到了


那么,今天带大家用Python来制作一个自动抢票的脚本小程序!

知识点:

  • 面向对象编程

  • selenium 操作浏览器

  • pickle 保存和读取Cookie实现免登陆

  • time 做延时操作

  • os 创建文件,判断文件是否存在

开发环境:

  • 版 本:anaconda5.2.0(python3.6.5)

  • 编辑器:pycharm

先导入本次所需的模块

import os
import time
import pickle
from time import sleep
from selenium import webdriver

第一步,实现免登录

确定目标,设置全局变量

damai_url = "就是那个网站"
login_url = "就是那个登陆地址"
target_url = '就是那个目标网站
#这里不是我不放,是平台不给放
#要全部的就来

初始化加载

class Concert:
def __init__(self):
 self.status = 0 
 self.login_method = 1 
 self.driver = webdriver.Chrome(executable_path='chromedriver.exe') 

登录调用设置cookie

def set_cookie(self):
 self.driver.get(damai_url)
print("###请点击登录###")
while self.driver.title.find('dm-全球演出赛事官方购票平台') != -1:
 sleep(1)
print('###请扫码登录###')

while self.driver.title != 'dm-全球演出赛事官方购票平台-100%正品、先付先抢、在线选座!':
 sleep(1)
print("###扫码成功###")
 pickle.dump(self.driver.get_cookies(), open("cookies.pkl", "wb"))
print("###Cookie保存成功###")
 self.driver.get(target_url

获取cookie

def get_cookie(self):
try:
 cookies = pickle.load(open("cookies.pkl", "rb")) 
for cookie in cookies:
 cookie_dict = {
'domain':'.damai.cn', 
'name': cookie.get('name'),
'value': cookie.get('value')
}
 self.driver.add_cookie(cookie_dict)
print('###载入Cookie###')
except Exception as e:
print(e)

登录

 def login(self):
if self.login_method==0:
 self.driver.get(login_url) 
print('###开始登录###')

elif self.login_method==1:
if not os.path.exists('cookies.pkl'): 
 self.set_cookie()
else:
 self.driver.get(target_url)
 self.get_cookie()

打开浏览器

def enter_concert(self):
"""打开浏览器"""
print('###打开浏览器,进入dm网###')

 self.login() 
 self.driver.refresh() 
 self.status = 2 
print("###登录成功###")
if self.isElementExist('/html/body/div[2]/div[2]/div/div/div[3]/div[2]'):
 self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div/div[3]/div[2]').click()

第二步,抢票并下单

判断元素是否存在

def isElementExist(self, element):
 flag = True
 browser = self.driver
try:
 browser.find_element_by_xpath(element)
return flag

except:
 flag = False
return flag

选票操作

def choose_ticket(self):
if self.status == 2: 
print("="*30)
print("###开始进行日期及票价选择###")
while self.driver.title.find('确认订单') == -1: 
try:
 buybutton = self.driver.find_element_by_class_name('buybtn').text
if buybutton == "提交缺货登记":

 self.status=2
 self.driver.get(target_url)
print('###抢票未开始,刷新等待开始###')
continue
elif buybutton == "立即预定":
 self.driver.find_element_by_class_name('buybtn').click()
 self.status = 3
elif buybutton == "立即购买":
 self.driver.find_element_by_class_name('buybtn').click()
 self.status = 4
elif buybutton == "选座购买":
 self.driver.find_element_by_class_name('buybtn').click()
 self.status = 5
except:
print('###未跳转到订单结算界面###')
 title = self.driver.title
if title == '选座购买':
 self.choice_seats()
elif title == '确认订单':
while True:
print('waiting ......')
if self.isElementExist('//*[@id="container"]/div/div[9]/button'):
 self.check_order()
break

选择座位

 def choice_seats(self):
while self.driver.title == '选座购买':
while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img'):
print('请快速的选择您的座位!!!')
while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[2]/div'):
 self.driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/button').click()

下单操作

def check_order(self):
if self.status in [3,4,5]:
print('###开始确认订单###')
try:
 self.driver.find_element_by_xpath('//*[@id="container"]/div/div[2]/div[2]/div[1]/div/label').click()
except Exception as e:
print("###购票人信息选中失败,自行查看元素位置###")
print(e)
 time.sleep(0.5)
 self.driver.find_element_by_xpath('//div[@class = "w1200"]//div[2]//div//div[9]//button[1]').click()

抢票完成,退出

def finish(self):
 self.driver.quit()

测试代码是否成功

if __name__ == '__main__':
try:
 con = Concert() 
 con.enter_concert() 
 con.choose_ticket() 

except Exception as e:
print(e)
 con.finish()

最后看下效果如何

这里是演示嗷,我买到票的时候忘记录了哈哈哈哈哈

最后

我真的庆幸还好我学了python ,该说不说,有点用的

从现在开始就准备去看演唱会噜~

关于Python技术储备

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


这份完整版的Python全套学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年5月20日
下一篇 2023年5月20日

相关推荐