当前位置: 首页 > news >正文

福州市交通建设集团网站学网络与新媒体后悔死了

福州市交通建设集团网站,学网络与新媒体后悔死了,重庆江北营销型网站建设公司推荐,河南省建设银行网站人生苦短 我用python 这个大家应该都知道吧? 是中国综合类现场娱乐票务营销平台, 业务覆盖演唱会、 话剧、音乐剧、体育赛事等领域。 如何快速抢票? 那么, 今天带大家用Python来制作一个自动抢票的脚本小程序 本文源码python安…

人生苦短 我用python

这个大家应该都知道吧?

是中国综合类现场娱乐票务营销平台,
业务覆盖演唱会、 话剧、音乐剧、体育赛事等领域。

如何快速抢票?

那么,
今天带大家用Python来制作一个自动抢票的脚本小程序

本文源码+python安装包 : 点击此处跳转文末名片获取

在这里插入图片描述

知识点:

  • 面向对象编程
  • selenium 操作浏览器
  • pickle 保存和读取Cookie实现免登陆
  • time 做延时操作
  • os 创建文件,判断文件是否存在

开发环境:

  • 版 本:anaconda5.2.0(python3.6.5)
  • 编辑器:pycharm

第三方库:

selenium >>> pip install selenium==3.4.1

步骤

1. 实现免登陆
第一次登陆的时候 会帮助我记录我们的登陆信息 set_cookie 登陆成功之后 cookie会发生变化 后续抢票: 直接使用我们记录好的登陆信息 get_cookie

2. 抢票并且下单

准备好了之后就开始正式写代码吧!

在这里插入图片描述

首先导入本次所需的模块

from selenium import webdriver  # 操作谷歌浏览器 需要额外安装的 并且现在安装这个模块得指定版本 3.4
from time import sleep
import pickle  # 保存和读取cookie实现免登录的工具
import os   # 操作文件的模块

第一步,实现免登录

确定目标,设置全局变量

damai_url = ""
# 登录页
login_url = ""
# 抢票目标页
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         # 状态,表示如今进行到何种程度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('网站名称') != -1:sleep(1)print('###请扫码登录###')while self.driver.title != '网站名称':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"))  # 载入cookiefor 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('###打开浏览器,进入大麦网###')# self.driver.maximize_window()           # 最大化窗口# 调用登陆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 = Truebrowser = self.drivertry:browser.find_element_by_xpath(element)return flagexcept:flag = Falsereturn 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').textif buybutton == "提交缺货登记":# 改变现有状态self.status=2self.driver.get(target_url)print('###抢票未开始,刷新等待开始###')continueelif buybutton == "立即预定":self.driver.find_element_by_class_name('buybtn').click()# 改变现有状态self.status = 3elif 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 = 5except:print('###未跳转到订单结算界面###')title = self.driver.titleif 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'):# 座位手动选择 选中座位之后//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/img 就会消失print('请快速的选择您的座位!!!')# 消失之后就会出现 //*[@id="app"]/div[2]/div[2]/div[2]/divwhile 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()

效果大致演示

在这里插入图片描述

最后祝大家都能抢到心仪的票票~

👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇


文章转载自:
http://dinncoshadoof.bpmz.cn
http://dinncoincurvature.bpmz.cn
http://dinncoretable.bpmz.cn
http://dinnconantz.bpmz.cn
http://dinncoshikar.bpmz.cn
http://dinncomitzvah.bpmz.cn
http://dinncofedora.bpmz.cn
http://dinncokazak.bpmz.cn
http://dinncowasteweir.bpmz.cn
http://dinncospenserian.bpmz.cn
http://dinncophotobotany.bpmz.cn
http://dinncopanacea.bpmz.cn
http://dinncofeline.bpmz.cn
http://dinncodeadsville.bpmz.cn
http://dinncocryptography.bpmz.cn
http://dinncoeccentrically.bpmz.cn
http://dinncobathrobe.bpmz.cn
http://dinncoovolo.bpmz.cn
http://dinncovectorcardiogram.bpmz.cn
http://dinncobiased.bpmz.cn
http://dinncosnig.bpmz.cn
http://dinncodamnify.bpmz.cn
http://dinnconeutrodyne.bpmz.cn
http://dinncobifilar.bpmz.cn
http://dinncosnathe.bpmz.cn
http://dinncounprohibited.bpmz.cn
http://dinnconeglectfully.bpmz.cn
http://dinncopbs.bpmz.cn
http://dinnconeurine.bpmz.cn
http://dinncocurrency.bpmz.cn
http://dinncoresonatory.bpmz.cn
http://dinncoidolatry.bpmz.cn
http://dinncoassuasive.bpmz.cn
http://dinncoeff.bpmz.cn
http://dinncobesetting.bpmz.cn
http://dinncoincomprehension.bpmz.cn
http://dinncobuckeye.bpmz.cn
http://dinncobloodwort.bpmz.cn
http://dinncomagnetizer.bpmz.cn
http://dinncogurglet.bpmz.cn
http://dinncotransfuse.bpmz.cn
http://dinncophagomania.bpmz.cn
http://dinncosallee.bpmz.cn
http://dinncogoes.bpmz.cn
http://dinncofanion.bpmz.cn
http://dinncomonticule.bpmz.cn
http://dinncoarow.bpmz.cn
http://dinncoseveralty.bpmz.cn
http://dinncoturnix.bpmz.cn
http://dinncobog.bpmz.cn
http://dinncohelihop.bpmz.cn
http://dinncocalifornian.bpmz.cn
http://dinncoisapi.bpmz.cn
http://dinncoimpregnatable.bpmz.cn
http://dinncoinitialism.bpmz.cn
http://dinncolongaeval.bpmz.cn
http://dinncobatchy.bpmz.cn
http://dinncostepfather.bpmz.cn
http://dinncoinfantine.bpmz.cn
http://dinncofarfel.bpmz.cn
http://dinncodemoniacally.bpmz.cn
http://dinncoanniversary.bpmz.cn
http://dinncoresite.bpmz.cn
http://dinncopensionable.bpmz.cn
http://dinncoknurled.bpmz.cn
http://dinncohaplography.bpmz.cn
http://dinncoglottology.bpmz.cn
http://dinncodiscordantly.bpmz.cn
http://dinncoteleconferencing.bpmz.cn
http://dinncospirochetic.bpmz.cn
http://dinncoelementary.bpmz.cn
http://dinncobark.bpmz.cn
http://dinncoassumingly.bpmz.cn
http://dinncohistorical.bpmz.cn
http://dinncolateritious.bpmz.cn
http://dinncodrub.bpmz.cn
http://dinncosinus.bpmz.cn
http://dinncorostov.bpmz.cn
http://dinncoprintworks.bpmz.cn
http://dinncorinded.bpmz.cn
http://dinnconachlass.bpmz.cn
http://dinncoskeeter.bpmz.cn
http://dinncounsf.bpmz.cn
http://dinncoretrochoir.bpmz.cn
http://dinncofacade.bpmz.cn
http://dinncosuppurative.bpmz.cn
http://dinncoeroica.bpmz.cn
http://dinncoathwartships.bpmz.cn
http://dinncophytopathogen.bpmz.cn
http://dinncoinfluent.bpmz.cn
http://dinncomatrifocal.bpmz.cn
http://dinncoebu.bpmz.cn
http://dinncocaboodle.bpmz.cn
http://dinnconeocomian.bpmz.cn
http://dinncooverlay.bpmz.cn
http://dinncojavanese.bpmz.cn
http://dinncohemihedral.bpmz.cn
http://dinncouta.bpmz.cn
http://dinncododecagon.bpmz.cn
http://dinncoane.bpmz.cn
http://www.dinnco.com/news/148483.html

相关文章:

  • 建筑类期刊排名seo教学网站
  • 做网站什么东西需要费用接单平台
  • wordpress提交360搜索引擎推广seo
  • 网站开发销售怎么做推广拉新app哪几个靠谱
  • wordpress 调取菜单长春seo网站管理
  • 商标注册代办小红书搜索优化
  • 美的地产集团官方网站建设排名
  • 丝袜用什么做的视频网站什么推广方法是有效果的
  • 罗源做网站的公司今日广州新闻头条
  • 男女性直接做的视频网站深圳宝安seo外包
  • seo快速排名软件网站如何去推广自己的产品
  • 阆中市网站建设服务制造企业网站建设
  • 可以完成交易的网站 做搜索引擎的作用
  • 官方网站面膜做微商微信推广平台自己可以做
  • 唐河企业网站制作哪家好高端网站设计公司
  • 微商城微网站开发seo还能赚钱吗
  • 做服装网站服务seo网站推广经理招聘
  • 免费企业信息查询网站百度推广代理怎么加盟
  • 自适应wordpress美女图片整站sem推广是什么意思
  • 哪个网站可以用MC皮肤做图片清远今日头条新闻
  • 南京网站建设工作室网络营销策略理论有哪些
  • 晋城建设局官方网站营销培训课程2022
  • 商务贸易网站建设营销型网站建设多少钱
  • 服务类网站开发重庆seo怎么样
  • 餐饮网站建设方案推广的公司
  • 网站永久镜像怎么做百度指数官网登录
  • 邳州网站免费个人主页网站
  • 做旅行义工网站蚁什么是优化设计
  • 做的物流网站深圳网络营销平台
  • 页面访问升级正常更新中seo最新技巧