想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本

前言

嗨喽!大家好,这里是魔王!!

大麦网,是中国综合类现场娱乐票务营销平台,业务覆盖演唱会、 话剧、音乐剧、体育赛事等领域。

但是因为票数有限,还有黄牛们不能丢了饭碗,所以导致了,很多人都抢不到票

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

想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本

; 此次知识点:

  • 面向对象编程
  • selenium 操作浏览器
  • pickle 保存和读取Cookie实现免登陆
  • time 做延时操作
  • os 创建文件,判断文件是否存在

开发环境:

  • 版 本:anaconda5.2.0(python3.6.5)
  • 编辑器:pycharm

python安装包 安装教程视频
pycharm 社区版 专业版 及 激活码
视频教程私我领取

第三方库:

  • selenium >>> pip install selenium==3.4.1

步骤

  1. 实现免登陆
    第一次登陆的时候 会帮助我记录我们的登陆信息
    set_cookie 登陆成功之后 cookie会发生变化
    后续抢票: 直接使用我们记录好的登陆信息
    get_cookie
  2. 抢票并且下单

想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本

; 首先导入本次所需的模块

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

第一步,实现免登录

确定目标,设置全局变量


damai_url = "https://www.damai.cn/"

login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F"

target_url = 'https://detail.damai.cn/item.htm?spm=a2oeg.search_category.0.0.77f24d15RWgT4o&id=654534889506&clicktitle=%E5%A4%A7%E4%BC%97%E7

初始化加载

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('大麦网-全球演出赛事官方购票平台') != -1:
        sleep(1)
    print('###请扫码登录###')

    while self.driver.title != '大麦网-全球演出赛事官方购票平台-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('###打开浏览器,进入大麦网###')

    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):
![想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230619/4a0b661782e24b8ebf46390fb2333452.gif)
    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制作一个自动抢票脚本

Original: https://blog.csdn.net/python56123/article/details/121516459
Author: 魔王不会哭
Title: 想去看演唱却总是抢不到票?教你用Python制作一个自动抢票脚本

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/668021/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球