為了順利買到演唱會的票用Python制作了自動搶票的腳本
發(fā)布日期:2021-12-25 02:32 | 文章來源:gibhub
知識點(diǎn):
- 面向?qū)ο缶幊?/li>
- selenium 操作瀏覽器
- pickle 保存和讀取Cookie實(shí)現(xiàn)免登陸
- time 做延時(shí)操作
- os 創(chuàng)建文件,判斷文件是否存在
開發(fā)環(huán)境:
- 版 本:anaconda5.2.0(python3.6.5)
- 編輯器:pycharm
【付費(fèi)VIP完整版】只要看了就能學(xué)會的教程,80集Python基礎(chǔ)入門視頻教學(xué)
點(diǎn)這里即可免費(fèi)在線觀看
先導(dǎo)入本次所需的模塊
import os import time import pickle from time import sleep from selenium import webdriver
第一步,實(shí)現(xiàn)免登錄
確定目標(biāo),設(shè)置全局變量
# 大麥網(wǎng)主頁 damai_url = "https://www.damai.cn/" # 登錄頁 login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F" # 搶票目標(biāo)頁 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# 狀態(tài),表示如今進(jìn)行到何種程度
self.login_method = 1# {0:模擬登錄,1:Cookie登錄}自行選擇登錄方式
self.driver = webdriver.Chrome(executable_path='chromedriver.exe') # 默認(rèn)Chrome瀏覽器
登錄調(diào)用設(shè)置cookie
def set_cookie(self):
self.driver.get(damai_url)
print("###請點(diǎn)擊登錄###")
while self.driver.title.find('大麥網(wǎng)-全球演出賽事官方購票平臺') != -1:
sleep(1)
print('###請掃碼登錄###')
while self.driver.title != '大麥網(wǎng)-全球演出賽事官方購票平臺-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")) # 載入cookie
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'):
# 如果不存在cookie.pkl,就獲取一下
self.set_cookie()
else:
self.driver.get(target_url)
self.get_cookie()
打開瀏覽器
def enter_concert(self):
"""打開瀏覽器"""
print('###打開瀏覽器,進(jìn)入大麥網(wǎng)###')
# self.driver.maximize_window() # 最大化窗口
# 調(diào)用登陸
self.login()# 先登錄再說
self.driver.refresh() # 刷新頁面
self.status = 2 # 登錄成功標(biāo)識
print("###登錄成功###")
# 后續(xù)德云社可以講
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("###開始進(jìn)行日期及票價(jià)選擇###")
while self.driver.title.find('確認(rèn)訂單') == -1: # 如果跳轉(zhuǎn)到了訂單結(jié)算界面就算這步成功了,否則繼續(xù)執(zhí)行此步
try:
buybutton = self.driver.find_element_by_class_name('buybtn').text
if buybutton == "提交缺貨登記":
# 改變現(xiàn)有狀態(tài)
self.status=2
self.driver.get(target_url)
print('###搶票未開始,刷新等待開始###')
continue
elif buybutton == "立即預(yù)定":
self.driver.find_element_by_class_name('buybtn').click()
# 改變現(xiàn)有狀態(tài)
self.status = 3
elif buybutton == "立即購買":
self.driver.find_element_by_class_name('buybtn').click()
# 改變現(xiàn)有狀態(tài)
self.status = 4
# 選座購買暫時(shí)無法完成自動化
elif buybutton == "選座購買":
self.driver.find_element_by_class_name('buybtn').click()
self.status = 5
except:
print('###未跳轉(zhuǎn)到訂單結(jié)算界面###')
title = self.driver.title
if title == '選座購買':
# 實(shí)現(xiàn)選座位購買的邏輯
self.choice_seats()
elif title == '確認(rèn)訂單':
while True:
# 如果標(biāo)題為確認(rèn)訂單
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('請快速的選擇您的座位?。。?)
# 消失之后就會出現(xiàn) //*[@id="app"]/div[2]/div[2]/div[2]/div
while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[2]/div'):
# 找到之后進(jìn)行點(diǎn)擊確認(rèn)選座
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('###開始確認(rèn)訂單###')
try:
# 默認(rèn)選第一個(gè)購票人信息
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) # 太快會影響加載,導(dǎo)致按鈕點(diǎn)擊無效
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() # 具體如果填寫請查看類中的初始化函數(shù) con.enter_concert()# 打開瀏覽器 con.choose_ticket()# 開始搶票 except Exception as e: print(e) con.finish()
最后看下效果如何

到此這篇關(guān)于為了順利買到演唱會的票用Python制作了自動搶票的腳本的文章就介紹到這了,更多相關(guān)Python 自動搶票內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信