赞
踩
相信想去周董演唱会的大家都用过大麦网抢票吧?
可是…抢不到啊!!!
该说不说 我抢到了
那么,今天带大家用Python来制作一个自动抢票的脚本小程序!
知识点:
开发环境:
先导入本次所需的模块
import os
import time
import pickle
from time import sleep
from selenium import webdriver
确定目标,设置全局变量
damai_url = "就是那个网站"
login_url = "就是那个登陆地址"
target_url = '就是那个目标网站
#这里不是我不放,是平台不给放
#要全部的就来
初始化加载
class Concert:
def __init__(self):
self.status = 0
self.login_method = 1
self.driver = webdriver.Chrome(executable_path='chromedriver.exe')
登录调用设置cookie
def set_cookie(self):
self.driver.get(damai_url)
print("###请点击登录###")
while self.driver.title.find('dm-全球演出赛事官方购票平台') != -1:
sleep(1)
print('###请扫码登录###')
while self.driver.title != 'dm-全球演出赛事官方购票平台-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"))
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'):
self.set_cookie()
else:
self.driver.get(target_url)
self.get_cookie()
打开浏览器
def enter_concert(self):
"""打开浏览器"""
print('###打开浏览器,进入dm网###')
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'):
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('//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 ,该说不说,有点用的
从现在开始就准备去看演唱会噜~
完整Python大麦抢票源代码,已经打包好了,朋友们如果需要可以扫描下方CSDN官方认证二维码免费领取!
如果你也喜欢编程,想通过学习Python转行、做副业或者提升工作效率,我也为大家整理了一份 【最新全套Python学习资料】一定对你有用!
对于0基础小白入门:
如果你是零基础小白,想快速入门Python是可以考虑的!
1、学习时间相对较短,学习内容更全面更集中
2、可以找到适合自己的学习方案
这份资料包含:Python安装包+激活码、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习等教程,带你从零开始系统性的学好Python!
点击免费领取《CSDN大礼包》:
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
用通俗易懂的漫画,来教你学习Python,让你更容易记住,并且不会枯燥乏味。
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
这份完整版的Python全套学习资料已经上传至CSDN官方,朋友们如果需要可以扫描下方二维码免费获取【保证100%免费】
以上全套资料已经为大家打包准备好了,希望对正在学习Python的你有所帮助!
如果你觉得这篇文章有帮助,可以点个赞呀~
我会坚持每天更新Python相关干货,分享自己的学习经验帮助想学习Python的朋友们少走弯路!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。