這篇文章主要介紹怎么用Python制作自動搶票的腳本,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
面向對象編程
selenium 操作瀏覽器
pickle 保存和讀取Cookie實現免登陸
time 做延時操作
os 創建文件,判斷文件是否存在
版 本:anaconda5.2.0(python3.6.5)
編輯器:pycharm
【付費VIP完整版】只要看了就能學會的教程,80集Python基礎入門視頻教學
點這里即可免費在線觀看
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瀏覽器
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)
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('###打開瀏覽器,進入大麥網###') # 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 = 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'): # 座位手動選擇 選中座位之后//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img 就會消失 print('請快速的選擇您的座位?。?!') # 消失之后就會出現 //*[@id="app"]/div[2]/div[2]/div[2]/div 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('###開始確認訂單###') 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制作自動搶票的腳本”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。