from selenium import webdriver
from time import sleep
import pickle#用于保持和读取cookie
import os
#大麦网主页i
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.1d9b6a63BQpyDW&id=638663502196&clicktitle=%E5%90%89%E6%9E%97%E7%9C%81%E5%BE%B7%E4%BA%91%E7%A4%BE%C2%B7%E7%BA%A2%E4%BA%8B%E4%BC%9A-%E3%80%8A%E7%9B%B8%E5%A3%B0%E5%A4%A7%E4%BC%9A%E3%80%8B-%E9%95%BF%E6%98%A5'
class concert:
#初始化加载
def __init__(self):
self.status = 0#状态码,表示当前操作执行到哪了
self.login_method = 1#0:模拟登录,1:免登录
self.driver = webdriver.Chrome(executable_path='chromedriver.exe')
def set_cookie(self):
#登录网站的函数,并且记录登录cookie
self.driver.get(damai_url)#打开大麦网主页
print('请点击登录')
# self.driver.title.find('大麦网-全球演出赛事官方购票平台')
while self.driver.title != '大麦网-全球演出赛事官方购票平台-100%正品、先付先抢、在线选座!':
sleep(1)
print('扫码成功')
pickle.dump(self.driver.get_cookies(name,value,domain),open('cookies.pkl','wb'))
print('cookies保存成功')
self.driver.get(target_url)
def get_cookie(self):
cookies=pickle.load(open('cookies.pkl','rb'))
for cookie in cookies:
cookie_dict={
'name':cookie.get('name'),
'value':cookie.get('value')
}
self.driver.add_cookie(cookie_dict)
print('cookie载入成功')
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'):
self.set_cookie()
else:
self.driver.get(target_url)
self.get_cookie()#登录信息载入浏览器
def enter_concert(self):
print('打开浏览器进入大麦网')
self.login()
self.driver.refresh()
self.status=2
print('登录成功')
def chose_ticket(self):
if self.status==2:
print('------------------')
print('请选日期和票价')
while self.driver.title.find('确认订单')==-1:
buybutton = self.driver.find_element_by_class_name('buybtn').text
if buybutton=='提交缺货登记':
#疯狂刷新页面
self.driver.get(target_url)
#self.driver.refresh()
print('抢票未开始,刷新等待')
continue #退出当前循环
elif buybutton=='立即预定':
#下单
self.driver.find_element_by_class_name('buybtn').click()
self.status=3
c
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
title=self.driver.title
if title=='选座购买':
#实现选座购买的逻辑
self.choice_seats()
elif title=='确认订单':
#如果标题为确认订单
#疯狂点击确认订单
while True:
print('等待确认订单')
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('请快速选择你的座位')
sleep(0.5)
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()
con.check_order()
def check_order(self):#检测订单
if self.status in [3,4,5]:
print('开始确认订单')
try:
self.driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div[2]/div[1]/div[1]').click()
sleep(0.5)
self.driver.find_element_by_xpath('//*[@id="container"]/div/div[9]/button').click()
except Exception as e:
print('失败')
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):
#退出程序
# return 0
if __name__ == '__main__':
con=concert()
con.enter_concert()
con.chose_ticket()
网站抢票案例
最新推荐文章于 2024-04-18 07:12:52 发布