為了順利買到演唱會(huì)的票用Python制作了自動(dòng)搶票的腳本
知識(shí)點(diǎn):
- 面向?qū)ο缶幊?/li>
- selenium 操作瀏覽器
- pickle 保存和讀取Cookie實(shí)現(xiàn)免登陸
- time 做延時(shí)操作
- os 創(chuàng)建文件,判斷文件是否存在
開(kāi)發(fā)環(huán)境:
- 版 本:anaconda5.2.0(python3.6.5)
- 編輯器:pycharm
【付費(fèi)VIP完整版】只要看了就能學(xué)會(huì)的教程,80集Python基礎(chǔ)入門視頻教學(xué)
先導(dǎo)入本次所需的模塊
import os import time import pickle from time import sleep from selenium import webdriver
第一步,實(shí)現(xiàn)免登錄
確定目標(biāo),設(shè)置全局變量
# 大麥網(wǎng)主頁(yè) damai_url = "https://www.damai.cn/" # 登錄頁(yè) login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F" # 搶票目標(biāo)頁(yè) 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("###請(qǐng)點(diǎn)擊登錄###") while self.driver.title.find('大麥網(wǎng)-全球演出賽事官方購(gòu)票平臺(tái)') != -1: sleep(1) print('###請(qǐng)掃碼登錄###') while self.driver.title != '大麥網(wǎng)-全球演出賽事官方購(gòu)票平臺(tái)-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('###開(kāi)始登錄###') 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()
打開(kāi)瀏覽器
def enter_concert(self): """打開(kāi)瀏覽器""" print('###打開(kāi)瀏覽器,進(jìn)入大麥網(wǎng)###') # self.driver.maximize_window() # 最大化窗口 # 調(diào)用登陸 self.login() # 先登錄再說(shuō) self.driver.refresh() # 刷新頁(yè)面 self.status = 2 # 登錄成功標(biāo)識(shí) 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("###開(kāi)始進(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('###搶票未開(kāi)始,刷新等待開(kāi)始###') continue elif buybutton == "立即預(yù)定": self.driver.find_element_by_class_name('buybtn').click() # 改變現(xiàn)有狀態(tài) self.status = 3 elif buybutton == "立即購(gòu)買": self.driver.find_element_by_class_name('buybtn').click() # 改變現(xiàn)有狀態(tài) self.status = 4 # 選座購(gòu)買暫時(shí)無(wú)法完成自動(dòng)化 elif buybutton == "選座購(gòu)買": self.driver.find_element_by_class_name('buybtn').click() self.status = 5 except: print('###未跳轉(zhuǎn)到訂單結(jié)算界面###') title = self.driver.title if title == '選座購(gòu)買': # 實(shí)現(xiàn)選座位購(gòu)買的邏輯 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 == '選座購(gòu)買': while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img'): # 座位手動(dòng)選擇 選中座位之后//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img 就會(huì)消失 print('請(qǐng)快速的選擇您的座位?。?!') # 消失之后就會(huì)出現(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('###開(kāi)始確認(rèn)訂單###') try: # 默認(rèn)選第一個(gè)購(gòu)票人信息 self.driver.find_element_by_xpath('//*[@id="container"]/div/div[2]/div[2]/div[1]/div/label').click() except Exception as e: print("###購(gòu)票人信息選中失敗,自行查看元素位置###") print(e) # 最后一步提交訂單 time.sleep(0.5) # 太快會(huì)影響加載,導(dǎo)致按鈕點(diǎn)擊無(wú)效 self.driver.find_element_by_xpath('//div[@class = "w1200"]//div[2]//div//div[9]//button[1]').click()
搶票完成,退出
def finish(self): self.driver.quit()
測(cè)試代碼是否成功
if __name__ == '__main__': try: con = Concert() # 具體如果填寫請(qǐng)查看類中的初始化函數(shù) con.enter_concert() # 打開(kāi)瀏覽器 con.choose_ticket() # 開(kāi)始搶票 except Exception as e: print(e) con.finish()
最后看下效果如何
到此這篇關(guān)于為了順利買到演唱會(huì)的票用Python制作了自動(dòng)搶票的腳本的文章就介紹到這了,更多相關(guān)Python 自動(dòng)搶票內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 命名規(guī)范知識(shí)點(diǎn)匯總
這里給大家分享的是在python開(kāi)發(fā)過(guò)程中需要注意的命名的規(guī)范的知識(shí)匯總,有需要的小伙伴可以查看下2020-02-02PyQt5+requests實(shí)現(xiàn)車票查詢工具
這篇文章主要為大家詳細(xì)介紹了PyQt5+requests實(shí)現(xiàn)車票查詢工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01python爬蟲搭配起B(yǎng)ilibili唧唧的流程分析
這篇文章主要介紹了python爬蟲搭配起B(yǎng)ilibili唧唧的流程分析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12使用OpenCV實(shí)現(xiàn)仿射變換—旋轉(zhuǎn)功能
這篇文章主要介紹了在OpenCV里實(shí)現(xiàn)仿射變換——旋轉(zhuǎn)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Python實(shí)現(xiàn)的基數(shù)排序算法原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的基數(shù)排序算法,簡(jiǎn)單說(shuō)明了基數(shù)排序的原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用基數(shù)排序的具體操作技巧,需要的朋友可以參考下2017-11-11Python字節(jié)單位轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇Python字節(jié)單位轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12