前言
大麥網(wǎng)是中國綜合類現(xiàn)場(chǎng)娛樂票務(wù)營銷平臺(tái),業(yè)務(wù)覆蓋演唱會(huì)、 話劇、音樂劇、體育賽事等領(lǐng)域
今天,我們要用代碼來實(shí)現(xiàn)他的購票過程
先來看看完成后的效果是怎么樣的
開發(fā)環(huán)境
- 版 本:anaconda(python3.8.8)
- 編輯器:pycharm
代碼實(shí)現(xiàn)步驟
- 實(shí)現(xiàn)免登陸
- 選座并且下單
實(shí)現(xiàn)免登陸
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.6ee64d156yMCV9&id=672706937093&clicktitle=%E7%95%99%E5%A3%B0%E7%8E%A9%E5%85%B72022%E3%80%8C%E6%97%B6%E9%97%B4%E7%9A%84%E8%B7%A8%E5%BA%A6%E3%80%8D%E5%B7%A1%E6%BC%94Vol%C2%B71%20%E9%95%BF%E6%B2%99%E7%AB%99'
初始化加載
from selenium import webdriver # 操作瀏覽器的工具
def __init__(self):
self.status = 0 # 狀態(tài), 表示當(dāng)前操作執(zhí)行到了哪個(gè)步驟
self.login_method = 1 # {0:模擬登錄, 1:cookie登錄}自行選擇登錄的方式
self.driver = webdriver.Chrome(executable_path='chromedriver.exe') # 當(dāng)前瀏覽器驅(qū)動(dòng)對(duì)象
登錄
def login(self):
if self.login_method == 0:
self.driver.get(login_url)
print('###開始登錄###')
elif self.login_method == 1:
# 創(chuàng)建文件夾, 文件是否存在
if not os.path.exists('cookies.pkl'):
self.set_cookies() # 沒有文件的情況下, 登錄一下
else:
self.driver.get(target_url) # 跳轉(zhuǎn)到搶票頁
self.get_cookie() # 并且登錄
cookies: 登錄網(wǎng)站時(shí)出現(xiàn)的
import time
def set_cookies(self):
self.driver.get(damai_url)
print('###請(qǐng)點(diǎn)擊登錄###')
# 我沒有點(diǎn)擊登錄,就會(huì)一直延時(shí)在首頁, 不會(huì)進(jìn)行跳轉(zhuǎn)
while self.driver.title.find('大麥網(wǎng)-全球演出賽事官方購票平臺(tái)') != -1:
sleep(1)
print('###請(qǐng)掃碼登錄###')
# 沒有登錄成功
while self.driver.title != '大麥網(wǎng)-全球演出賽事官方購票平臺(tái)-100%正品、先付先搶、在線選座!':
sleep(1)
print('###掃碼成功###')
# get_cookies: driver里面的方法
pickle.dump(self.driver.get_cookies(), open('cookies.pkl', 'wb'))
print('###cookie保存成功###')
self.driver.get(target_url)
假如說我現(xiàn)在本地有 cookies.pkl 那么 直接獲取
def get_cookie(self):
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###')
運(yùn)行代碼可以得到所需要的cookis,這樣就可以實(shí)現(xiàn)免登錄文章來源:http://www.zghlxwxcb.cn/news/detail-412731.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-412731.html
打開瀏覽器
def enter_concert(self):
print('###打開瀏覽器,進(jìn)入大麥網(wǎng)###')
# 調(diào)用登錄
self.login() # 先登錄再說
self.driver.refresh() # 刷新頁面
self.status = 2 # 登錄成功標(biāo)識(shí)
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()
實(shí)現(xiàn)購票
選票操作
def choose_ticket(self):
if self.status == 2:
print('=' * 30)
print('###開始進(jìn)行日期及票價(jià)選擇###')
while self.driver.title.find("確認(rèn)訂單") == -1:
try:
buybutton = self.driver.find_element_by_class_name('buybtn').text
if buybutton == '提交缺貨登記':
self.status = 2 # 沒有進(jìn)行更改操作
self.driver.get(target_url) # 刷新頁面 繼續(xù)執(zhí)行操作
elif buybutton == '立即預(yù)定':
# 點(diǎn)擊立即預(yù)定
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('###沒有跳轉(zhuǎn)到訂單結(jié)算界面###')
title = self.driver.title
if title == '選座購買':
# 選座購買的邏輯
self.choice_seats()
elif title == '確認(rèn)訂單':
# 實(shí)現(xiàn)下單的邏輯
while True:
# 如果標(biāo)題為確認(rèn)訂單
print('正在加載.......')
# 如果當(dāng)前購票人信息存在 就點(diǎn)擊
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('請(qǐng)快速選擇你想要的座位!!!')
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('###開始確認(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('//*[@id="container"]/div/div[9]/button').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 finish(self):
self.driver.quit()
到了這里,關(guān)于Python selenium 簡(jiǎn)單的實(shí)現(xiàn)大麥網(wǎng)自動(dòng)購票過程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!