Python selenium 实现大麦网自动购票过程
一些无关紧要的哔哔:
大麦网是中国综合类现场娱乐票务营销平台,业务覆盖演唱会、 话剧、音乐剧、体育赛事等领域
今天,我们要用代码来实现他的购票过程
开搞!

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

开发环境
- 版 本:anaconda(python3.8.8)
- 编辑器:pycharm
代码实现步骤
- 实现免登陆
- 选座并且下单
实现免登陆
###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
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.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'
初始化加载
from selenium import webdriver # 操作浏览器的工具
def __init__(self):
self.status = 0 # 状态, 表示当前操作执行到了哪个步骤
self.login_method = 1 # {0:模拟登录, 1:cookie登录}自行选择登录的方式
self.driver = webdriver.Chrome(executable_path='chromedriver.exe') # 当前浏览器驱动对象
登录
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_cookies() # 没有文件的情况下, 登录一下
else:
self.driver.get(target_url) # 跳转到抢票页
self.get_cookie() # 并且登录
cookies: 登录网站时出现的
###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
import time
def set_cookies(self):
self.driver.get(damai_url)
print('###请点击登录###')
# 我没有点击登录,就会一直延时在首页, 不会进行跳转
while self.driver.title.find('大麦网-全球演出赛事官方购票平台') != -1:
sleep(1)
print('###请扫码登录###')
# 没有登录成功
while self.driver.title != '大麦网-全球演出赛事官方购票平台-100%正品、先付先抢、在线选座!':
sleep(1)
print('###扫码成功###')
# get_cookies: driver里面的方法
pickle.dump(self.driver.get_cookies(), open('cookies.pkl', 'wb'))
print('###cookie保存成功###')
self.driver.get(target_url)
假如说我现在本地有 cookies.pkl 那么 直接获取
###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
def get_cookie(self):
cookies = pickle.load(open('cookies.pkl', 'rb'))
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###')
运行代码可以得到所需要的cookis,这样就可以实现免登录
打开浏览器
###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
def enter_concert(self):
print('###打开浏览器,进入大麦网###')
# 调用登录
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()
实现购票
选票操作
###想要学习Python?Python学习交流群:660193417 满足你的需求,资料都已经上传群文件,可以自行下载!###
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) # 刷新页面 继续执行操作
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('正在加载.......')
# 如果当前购票人信息存在 就点击
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('请快速选择你想要的座位!!!')
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('//*[@id="container"]/div/div[9]/button').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 finish(self):
self.driver.quit()
Python selenium 实现大麦网自动购票过程的更多相关文章
- python+selenium生成测试报告后自动发送邮件
标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...
- python+selenium+webdriver+BeautifulSoup实现自动登录
from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...
- python+selenium 模拟登陆,自动下单
目前写的实在太粗糙,留着,以后来写上
- Python selenium Chrome正在受到自动软件的控制 disable-infobars无效 的解决方法
问题解决 前两天更新了google浏览器版本,今天运行以前的脚本,发现options一个参数的配置不生效了. 运行了几次都发现该参数没有生效,也检查了自己的代码参数,没有写错,于是就有了这一波“网中寻 ...
- Python+Selenium学习笔记19 - 自动发送邮件
发送简单的邮件 用一个QQ邮箱发送到另一个QQ邮件. 首先设置QQ邮箱,邮箱设置 -> 账号 开启SMTP服务,点击开启按钮,按提示进行操作,需要1毛钱的短信费.开启后如下所示 1 # codi ...
- python+splinter实现12306网站刷票并自动购票流程
python+splinter实现12306网站刷票并自动购票流程 通过python+splinter,实现在12306网站刷票并自动购票流程(无法自动识别验证码). 此类程序只是提高了12306网站 ...
- Python + Selenium 自动发布文章(一):开源中国
https://blog.csdn.net/qq_28804275/article/details/80891949 https://blog.csdn.net/qq_28804275/article ...
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
写在前面 爬虫和自动测试,对于python来说是最合适不过也是最擅长的. 开源的项目也很多,例如you-get项目https://github.com/soimort/you-get.盗链和爬虫神器. ...
- python+selenium自动测试之WebDriver的常用API(基础篇二)
本篇介绍一下python+selenium复杂操作的处理,基于python3.6,selenium3.141,详细资料介绍查看官方API文档,点击这里 一.常见特殊情况处理如iframe/弹窗处理 有 ...
随机推荐
- 通过循环按行顺序为一个5×5的二维数组a赋1到25的自然数,然后输出该数组。试编程。
- 论文阅读 Continuous-Time Dynamic Network Embeddings
1 Continuous-Time Dynamic Network Embeddings Abstract 描述一种将时间信息纳入网络嵌入的通用框架,该框架提出了从CTDG中学习时间相关嵌入 Co ...
- 技术分享 | WEB 端常见 Bug 解析
对于 WEB 产品来说,有一些常见的 Bug,本章节挑选一些比较典型的 Bug 进行举例介绍. UI Bug 页面展示的时候,需要根据长度的边界值去设计用例进行验证. 一般来说都会有超长内容的验证 ...
- myeclipse10的问题
1.导入项目后java文件头部出现The type java.lang.CharSequence cannot be resolved. It is indirectly referenced fro ...
- 单列集合(Collection-List)
与数组的区别 ArrayList while循环快捷键itit 遍历方法2:增强for循环 快捷键大写的I List接口(少部分常用的) List三种遍历方式 注意事项 ArrrayList底层结构和 ...
- 一篇讲清楚String、StringBuffer和StringBuild
一.String篇 1.String基本介绍? (jdk文档原文)String类代表字符串. Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例. 说人 ...
- 入行IT,一定要会Linux吗?
现在是21世纪,是科学技术大力发展的一个时代,IT行业已经成为现在的一个非常热门的一个行业,许许多多的人都想要往IT方面发展,找IT方面相关的一个工作.很多想要接触IT行业的初学者伤透了脑筋,我该学什 ...
- ceph日常运维管理
点击关注上方"开源Linux", 后台回复"读书",有我为您特别筛选书籍资料~ 相关阅读: ceph分布式存储简介 常见问题 nearfull osd(s) o ...
- 【面试普通人VS高手系列】Spring中事务的传播行为有哪些?
一个工作了2年的粉丝,私信了一个比较简单的问题. 说: "Spring中事务的传播行为有哪些?" 他说他能记得一些,但是在项目中基本上不需要配置,所以一下就忘记了. 结果导致面试被 ...
- mybatis 查询返回的类型中字段类型为 List<xx>
基本类型数组 mapper.xml <resultMap id="xxDtoResultMap" type="com.xx.xxDto"> < ...