周董演唱会为什么总是抢不到票?教你用Python做一个自动抢票脚本

嗨害大家好鸭!我是小熊猫~

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

可是

抢不到啊

该说不说 我抢到了

本文源码、其他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

本文源码、其他python资料电子书:点击此处跳转文末名片获取

第一步,实现免登录

确定目标,设置全局变量

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 ,该说不说,有点用的

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

在这里插入图片描述

👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

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

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年3月8日 下午8:36
下一篇 2023年3月8日 下午8:37

相关推荐