0


Python selenium 简单的实现大麦网自动购票过程

目录

前言

大麦网是中国综合类现场娱乐票务营销平台,业务覆盖演唱会、 话剧、音乐剧、体育赛事等领域
今天,我们要用代码来实现他的购票过程

先来看看完成后的效果是怎么样的

开发环境

  • 版 本:anaconda(python3.8.8)
  • 编辑器:pycharm

代码实现步骤

  • 实现免登陆
  • 选座并且下单

实现免登陆

  1. damai_url ='https://www.damai.cn/'# 登录
  2. login_url ='https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F'# 抢票目标页
  3. 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'

初始化加载

  1. from selenium import webdriver # 操作浏览器的工具def__init__(self):
  2. self.status =0# 状态, 表示当前操作执行到了哪个步骤
  3. self.login_method =1# {0:模拟登录, 1:cookie登录}自行选择登录的方式
  4. self.driver = webdriver.Chrome(executable_path='chromedriver.exe')# 当前浏览器驱动对象

登录

  1. deflogin(self):if self.login_method ==0:
  2. self.driver.get(login_url)print('###开始登录###')elif self.login_method ==1:# 创建文件夹, 文件是否存在ifnot os.path.exists('cookies.pkl'):
  3. self.set_cookies()# 没有文件的情况下, 登录一下else:
  4. self.driver.get(target_url)# 跳转到抢票页
  5. self.get_cookie()# 并且登录

cookies: 登录网站时出现的

  1. import time
  2. defset_cookies(self):
  3. self.driver.get(damai_url)print('###请点击登录###')# 我没有点击登录,就会一直延时在首页, 不会进行跳转while self.driver.title.find('大麦网-全球演出赛事官方购票平台')!=-1:
  4. sleep(1)print('###请扫码登录###')# 没有登录成功while self.driver.title !='大麦网-全球演出赛事官方购票平台-100%正品、先付先抢、在线选座!':
  5. sleep(1)print('###扫码成功###')# get_cookies: driver里面的方法
  6. pickle.dump(self.driver.get_cookies(),open('cookies.pkl','wb'))print('###cookie保存成功###')
  7. self.driver.get(target_url)

假如说我现在本地有 cookies.pkl 那么 直接获取

  1. defget_cookie(self):
  2. cookies = pickle.load(open('cookies.pkl','rb'))for cookie in cookies:
  3. cookie_dict ={'domain':'.damai.cn',# 必须要有的, 否则就是假登录'name': cookie.get('name'),'value': cookie.get('value')}
  4. self.driver.add_cookie(cookie_dict)print('###载入cookie###')

运行代码可以得到所需要的cookis,这样就可以实现免登录

打开浏览器

  1. defenter_concert(self):print('###打开浏览器,进入大麦网###')# 调用登录
  2. self.login()# 先登录再说
  3. self.driver.refresh()# 刷新页面
  4. self.status =2# 登录成功标识print('###登录成功###')# 处理弹窗if self.isElementExist('/html/body/div[2]/div[2]/div/div/div[3]/div[2]'):
  5. self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div/div[3]/div[2]').click()

实现购票

选票操作

  1. defchoose_ticket(self):if self.status ==2:print('='*30)print('###开始进行日期及票价选择###')while self.driver.title.find("确认订单")==-1:try:
  2. buybutton = self.driver.find_element_by_class_name('buybtn').text
  3. if buybutton =='提交缺货登记':
  4. self.status =2# 没有进行更改操作
  5. self.driver.get(target_url)# 刷新页面 继续执行操作elif buybutton =='立即预定':# 点击立即预定
  6. self.driver.find_element_by_class_name('buybtn').click()
  7. self.status =3elif buybutton =='立即购买':
  8. self.driver.find_element_by_class_name('buybtn').click()
  9. self.status =4elif buybutton =='选座购买':
  10. self.driver.find_element_by_class_name('buybtn').click()
  11. self.status =5except:print('###没有跳转到订单结算界面###')
  12. title = self.driver.title
  13. if title =='选座购买':# 选座购买的逻辑
  14. self.choice_seats()elif title =='确认订单':# 实现下单的逻辑whileTrue:# 如果标题为确认订单print('正在加载.......')# 如果当前购票人信息存在 就点击if self.isElementExist('//*[@id="container"]/div/div[9]/button'):# 下单操作
  15. self.check_order()break

选择座位

  1. defchoice_seats(self):while self.driver.title =='选座购买':while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img'):print('请快速选择你想要的座位!!!')while self.isElementExist('//*[@id="app"]/div[2]/div[2]/div[2]/div'):
  2. self.driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[2]/button').click()

下单操作

  1. defcheck_order(self):if self.status in[3,4,5]:print('###开始确认订单###')try:# 默认选第一个购票人信息
  2. 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)# 最后一步提交订单
  3. time.sleep(0.5)# 太快了不好, 影响加载 导致按钮点击无效
  4. self.driver.find_element_by_xpath('//*[@id="container"]/div/div[9]/button').click()

判断元素是否存在

  1. defisElementExist(self, element):
  2. flag =True
  3. browser = self.driver
  4. try:
  5. browser.find_element_by_xpath(element)return flag
  6. except:
  7. flag =Falsereturn flag

购票完成, 退出

  1. deffinish(self):
  2. self.driver.quit()
标签: python selenium pycharm

本文转载自: https://blog.csdn.net/m0_48405781/article/details/124672784
版权归原作者 松鼠爱吃饼干 所有, 如有侵权,请联系我们删除。

“Python selenium 简单的实现大麦网自动购票过程”的评论:

还没有评论