In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use Python to make a script for automatic ticket grabbing, which is very detailed and has a certain reference value. Interested friends must finish reading it!
Knowledge points:
Object oriented programming
Selenium Operation browser
Pickle saves and reads Cookie to achieve login-free
Time does delay operation.
Os creates a file to determine whether the file exists
Development environment:
Version: anaconda5.2.0 (python3.6.5)
Editor: pycharm
[full version of paid VIP] A course that you can learn as long as you read it, 80 episodes of Python basic introductory video teaching
Click here to watch it online for free.
The first step is to import the required module import osimport timeimport picklefrom timeimport sleepfrom selenium import webdriver to achieve the goal of login-free. Set the global variable # barley home page damai_url = "https://www.damai.cn/"# login page login_url =" https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F"# ticket grabbing target page 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 Initialize loading class Concert: def _ _ init__ (self): self.status = 0 # status Indicates how far it is now self.login_method = 1 # {0: simulated login 1:Cookie login} choose your own login method self.driver = webdriver.Chrome (executable_path='chromedriver.exe') # default Chrome browser login settings cookiedef set_cookie (self): self.driver.get (damai_url) print ("# Please click to log in # #") while self.driver.title.find ('Barley net-official ticketing platform for global performance events' )! =-1: sleep (1) print ('# Please scan the code to log in # #) while self.driver.title! = 'Barley net-official ticketing platform for global performance events-100% authentic, Pay first, grab first, and choose seats online!' : sleep (1) print ("# scan successfully #") pickle.dump (self.driver.get_cookies (), open ("cookies.pkl", "wb")) print ("# Cookie saved successfully #") self.driver.get (target_url) get cookiedef get_cookie (self): try: cookies = pickle.load (open ("cookies.pkl") "rb")) # load cookie for cookie in cookies: cookie_dict = {'domain':'.damai.cn', # must have Otherwise, it is a fake login to 'name': cookie.get (' name'). 'value': cookie.get (' value')} self.driver.add_cookie (cookie_dict) print ('# load Cookie###') except Exception as e: print (e) login def login (self): if self.login_method==0: self.driver.get (login_url) # load login interface print ('# start login #') elif self.login_method==1: if not os.path.exists ('cookies.pkl'): # if cookie.pkl does not exist Just get self.set_cookie () else: self.driver.get (target_url) self.get_cookie () Open browser def enter_concert (self): "" Open browser "" print ('# Open browser " Enter the barley network #) # self.driver.maximize_window () # maximize window # call login self.login () # log in first and say self.driver.refresh () # refresh page self.status = 2 # login Function logo print ("# login successful #") # follow up Deyun Society can say 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 () second step Snatch tickets and place an order to determine whether the element exists def isElementExist (self Element): flag = True browser = self.driver try: browser.find_element_by_xpath (element) return flag except: flag = False return flag ballot operation def choose_ticket (self): if self.status = = 2: # login success entry print ("=" * 30) print ("# start date and Ticket price selection # # ") while self.driver.title.find ('confirm order') =-1: # if you jump to the order settlement interface, this step is considered successful Otherwise, continue with this step try: buybutton = self.driver.find_element_by_class_name ('buybtn'). Text if buybutton = = "submit out-of-stock registration": # change the existing status self.status=2 self.driver.get (target_url) Print ('# ticket scramble has not started Refresh waiting starts #') continue elif buybutton = "book now": self.driver.find_element_by_class_name ('buybtn') .click () # change the existing status self.status = 3 elif buybutton = = "Buy now" : self.driver.find_element_by_class_name ('buybtn'). Click () # change the existing status self.status = 4 # seat selection purchase is temporarily unable to complete automation elif buybutton = = "seat purchase": self.driver .find _ element_by_class_name ('buybtn'). Click () self.status = 5 except: print (' # did not jump to the order settlement interface # #) title = self.driver.title if title = = 'choose a seat to purchase': # realize the logic of choosing a seat to purchase Self.choice_seats () elif title = = 'confirm order': while True: # if the title is confirm order print ('waiting.') If self.isElementExist ('/ * [@ id= "container"] / div/div [9] / button'): self.check_order () break Select seat def choice_seats (self): while self.driver.title = = 'Buy seat': while self.isElementExist ('/ * [@ id= "app"] / div [2] / div [ 2] / div [1] / div [2] / img'): # after manually selecting a seat / / * [@ id= "app"] / div [2] / div [2] / div [1] / div [2] / img will disappear print ('Please choose your seat quickly!') / / * [@ id= "app"] / div [2] / div while self.isElementExist ('/ / * [@ id= "app"] / div [2] / div [2] / div'): # Click to confirm the seat self.driver.find_element_by_xpath after finding it ('/ / * [@ id= "app"] / div [2] / button') .click () place an order operation def check_order (self): if self.status in [3jue 4 5]: print ('# start confirming order #') try: # choose the first buyer information self.driver.find_element_by_xpath ('/ / * [@ id= "container"] / div/div [2] / div [2] / div [1] / div/label'). Click () except Exception as e: Print ("# failed to select ticket buyer information Check element location # ") print (e) # Last step submit order time.sleep (0.5) # too soon will affect loading, resulting in invalid self.driver.find_element_by_xpath ('/ / div [@ class =" w1200 "] / / div [2] / / div//div [9] / / button [1]'). Click () Exit def finish (self): self.driver.quit () tests whether the code is successful if _ _ name__ = ='_ main__': try: con = Concert () # if you enter it, please check the initialization function con.enter_concert () # Open browser con.choose_ticket () # Start grabbing tickets except Exception as e: print (e) con.finish () finally see how it works.
The above is all the contents of this article "how to use Python to make a script for automatic ticket grabbing". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.