人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

為了順利買到演唱會的票用Python制作了自動搶票的腳本

發(fā)布日期:2021-12-25 02:32 | 文章來源:gibhub

知識點:

  • 面向對象編程
  • selenium 操作瀏覽器
  • pickle 保存和讀取Cookie實現(xiàn)免登陸
  • time 做延時操作
  • os 創(chuàng)建文件,判斷文件是否存在

開發(fā)環(huán)境:

  • 版 本:anaconda5.2.0(python3.6.5)
  • 編輯器:pycharm

【付費VIP完整版】只要看了就能學會的教程,80集Python基礎入門視頻教學

點這里即可免費在線觀看

先導入本次所需的模塊

import os
import time
import pickle
from time import sleep
from selenium import webdriver

第一步,實現(xiàn)免登錄

確定目標,設置全局變量

# 大麥網(wǎng)主頁
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# 狀態(tài),表示如今進行到何種程度
  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('大麥網(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('###打開瀏覽器,進入大麥網(wǎng)###')
 # self.driver.maximize_window()  # 最大化窗口
 # 調用登陸
 self.login()# 先登錄再說
 self.driver.refresh() # 刷新頁面
 self.status = 2 # 登錄成功標識
 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("###開始進行日期及票價選擇###")
  while self.driver.title.find('確認訂單') == -1:  # 如果跳轉到了訂單結算界面就算這步成功了,否則繼續(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 == "立即預定":
  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
 # 選座購買暫時無法完成自動化
 elif buybutton == "選座購買":
  self.driver.find_element_by_class_name('buybtn').click()
  self.status = 5
except:
 print('###未跳轉到訂單結算界面###')
title = self.driver.title
if title == '選座購買':
 # 實現(xiàn)選座位購買的邏輯
 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('請快速的選擇您的座位?。。?)
# 消失之后就會出現(xiàn) //*[@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() # 具體如果填寫請查看類中的初始化函數(shù)
  con.enter_concert()# 打開瀏覽器
  con.choose_ticket()# 開始搶票
 except Exception as e:
  print(e)
  con.finish()

最后看下效果如何

到此這篇關于為了順利買到演唱會的票用Python制作了自動搶票的腳本的文章就介紹到這了,更多相關Python 自動搶票內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!

版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部