50 个加速包都抢不到车票,还不如这个 Python 抢票神器!
前言
五一劳动节即将到来,许多人已经开始计划自己的假期。然而今年的五一假期可能会更加紧张和有挑战性。抢票成为了许多人最为关注的问题之一。在这篇文章中,我们将探讨一些抢票的技巧和策略,帮助大家更好地应对五一抢票的挑战。
目录
-
-
- 前言
-
- 一、开发环境
-
- 二、实现免登录
-
- 三、抢票并下单
-
- 四、测试代码是否成功
-
- 看下实现效果
- 最后
一、开发环境
- 工具:python
- 编辑器:pycharm
首先导入本次所需的模块
import os
import time
import pickle
from time import sleep
from selenium import webdriver
本文模块\\环境\\源码\\教程皆可点击此处跳转免费领
二、实现免登录
- 确定目标,设置全局变量
# 大麦网主页
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 # {0:模拟登录,1:Cookie登录}自行选择登录方式self.driver = webdriver.Chrome(executable_path='chromedriver.exe') # 默认Chrome浏览器
- 登录调用设置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")) # 载入cookiefor 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'): # 如果不存在cookie.pkl,就获取一下self.set_cookie()else:self.driver.get(target_url)self.get_cookie()
- 打开浏览器
def enter_concert(self):"""打开浏览器"""print('打开浏览器,进入购票网')# self.driver.maximize_window() # 最大化窗口# 调用登陆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 = Truebrowser = self.drivertry:browser.find_element_by_xpath(element)return flagexcept:flag = Falsereturn 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').textif buybutton == "提交缺货登记":# 改变现有状态self.status=2self.driver.get(target_url)print('抢票未开始,刷新等待开始')continueelif buybutton == "立即预定":self.driver.find_element_by_class_name('buybtn').click()# 改变现有状态self.status = 3elif 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 = 5except:print('未跳转到订单结算界面')title = self.driver.titleif 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'):# 座位手动选择 选中座位之后//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img 就会消失print('请快速的选择您的座位!!!')# 消失之后就会出现 //*[@id="app"]/div[2]/div[2]/div[2]/divwhile 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()
看下实现效果
最后
👇👇👇课件源码、资料、素材、解答、皆点击下方获取呀👇👇👇
本文所有模块\\环境\\源码\\教程皆可点击此处跳转免费领
最后最后最后祝你们五一假期玩的开心啦