当前位置:网站首页>Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)

Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)

2022-04-23 20:21:00 Install a sound 77

The test object is barley net 2020 Shanghai Lin Junjie concert , For everyone's concern about whether they can grab tickets . Start with the conclusion

1. With the current script , Without a lot of optimization Depend on python The speed to compete with ticket dealers is impossible .

2. This project is not suitable for pure white , because selenium Too many pits , But bloggers, who have some front-end foundation, still gain a lot .

3. This project comes from Zhihu users Oliver0047, place https://zhuanlan.zhihu.com/p/56697166, Due to time , The front-end format of barley web has changed a lot , Therefore, the main content needs to be changed according to the situation , But the principle is the same .

4. Online   Two Tickets... Shanghai Concert of Lin Junjie , The unit price 780 The following contact me !!!

————————————— The following is the main content —————————————————

1. selenium Installation and use

selenium It's a packaged web Automation library , Used to simulate user operation , The focus of this project is to simulate the process , That is, using crawlers and Automation , Log in your usual , Click the button and other behaviors turn into py Code operation .

First default python packed , If you use pycharm Directly in setting Found in selenium Can be installed

In addition, you need to install the corresponding driver, Recommended Chrome And fox , If you want to install Google Chromedriver

And depending on the browser version ,Chromedriver It's also a different version , Find the version number in the settings and download it accordingly .

And compare it with the... Of the script py Put the files in the same directory .

After installation, first import the library , And will

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
import time
import pickle
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Load the landing page this time 
login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F"

# Use this command to access , In the complete code, I will log off the following three lines 
driver = webdriver.Chrome()  
driver.maximize_window()
driver.get(login_url)

The results are shown in the following figure , When you find something above Chrome When under the control of automatic software , Description: it has been enabled successfully , We will log in from this page .

 

2. Platform account login

The original author wrote everything in one class below , Very clear , Worth learning .

class Concert(object):
    def __init__(self, name, date, price, place, real_name, method=1):
        self.name = name  #  singsing star 
        self.date = date  #  Date sequence number priority , such as , If the second time is feasible , Just choose the second one 
        self.price = price  #  Priority of ticket number , Same as above 
        self.place = place  #  place 
        self.status = 0  #  state , To what extent is it going on now 
        self.login_method = 1  # {0: Simulated Login ,1:Cookie Sign in } Choose your own login method 
        self.real_name = real_name  #  Serial number of real name person 

Automatic login for account , The original author gave two methods : First, simulate account input and login ; The two is the use of cookie land .

First, simulate the account and click to log in ; The two is the use of cookie land .

Method 1 has been tested and can no longer be used , The reason is the anti climbing ability of barley net , When it detects that under the browser selenium Next time , You can't log in to the inside , Of course, there are some tutorials on the Internet to bypass detection https://www.jianshu.com/p/4dd2737a3048 Interested students can study .

Method 2 , It's through Scan the code and log in Recorded cookie Realize automatic login of information , Records of the cookie The information may need to be regenerated for some time .

    # Generate cookie Function of 
    def get_cookie(self):
        self.driver.get(login_url)
        # Switch to iframe Content 
        self.driver.switch_to.frame("alibaba-login-box");
        # Click scan code to log in 
        self.driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[3]").click()
        # Start scanning 
        sleep(10)
        # preservation cookie
        pickle.dump(self.driver.get_cookies(), open("cookies.pkl", "wb"))
        print("###Cookie Saved successfully ###")

There's a little dot , Another page is built into the login page . So you can't directly crawl through the whole web page , You need to use it first self.driver.switch_to.frame Navigate to the built-in page , Then locate to the code scanning place .

    # Load function 
    def set_cookie(self):
        try:
            cookies = pickle.load(open("cookies.pkl", "rb"))  #  load cookie
            for cookie in cookies:
                cookie_dict = {
                    'domain': '.damai.cn',  #  There has to be , Or it's a fake login 
                    'name': cookie.get('name'),
                    'value': cookie.get('value'),
                    "expires": "",
                    'path': '/',
                    'httpOnly': False,
                    'HostOnly': False,
                    'Secure': False}
                # load cookie
                self.driver.add_cookie(cookie_dict)

Login code , Just get and load two steps .

    # Login function 
    def login(self):
        if self.login_method == 1:
            if not os.path.exists('cookies.pkl'):  #  If it doesn't exist cookie.pkl, Just get it 
                self.get_cookie()
            else:
                self.driver.get(login_url)
                self.set_cookie()

3. Concert selection and ticket purchase

Next , It's mainly about reptiles , Specifically, locate the search box , Fill in the content , Then click something , There's nothing special to say , About how to position , Suggest using Xpath Full positioning and class location , As for page structure utilize CTRL+SHIFT+C, Move to the component you want to use to locate it , stay F12 Right click inside copy Xpath You can get the position , Sometimes one div There's more than one of the same class, Then you need to use hierarchical positioning , First to the parent and then to the child below .

    def enter_concert(self):
        print('### Open the browser , Enter barley net ###')
        self.driver = webdriver.Chrome()  #  Google browser 
        self.driver.maximize_window()
        self.login()  #  Log in first 
        self.driver.get("https://www.damai.cn/")

        try:
            # Mainly to verify whether the login is successful , User name check 
            locator = (By.XPATH,"/html/body/div[2]/div/div[3]/div[1]/a[2]/div")
            # According to wait 3s Until the verification is successful 
            element = WebDriverWait(self.driver,3).until(EC.text_to_be_present_in_element(locator, self.usr_name))
            self.status = 1
            print("### Login successful ###")

        except Exception as d:
            print(d)

        if self.status == 1:
            self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[4]/input')[0].send_keys(self.name)  #  Enter the singer in the search bar 
            self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[4]/div[1]')[0].click()  #  Click on the search 
            self.driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div/div/span')[0].click()
            
            #  It was troublesome to make some cuts to the original code 
            #  Get all possible concerts 
            titles = []
            links = []
            #  The annotated code indicates that the concert is manually selected with the graphical interface , You can experience 
            #        root = Tk()
            #        root.title(" Choose a concert ")
            #        v = IntVar()
            #        v.set(1)
            self.choose_result = 0
            #        def selection():
            #            self.choose_result=v.get()
            #            root.destroy()

            #  The meaning here is to navigate to the purchase page url, And the location of the concert meet the expectations 
            lists = self.driver.find_elements_by_class_name('items__txt__title')
            i=0
            for li in lists:
                word_link=li
                        #titles.append(word_link.text)
                print(word_link)
                temp_s = word_link.get_attribute('innerHTML').find('href')+8
                temp_e = word_link.get_attribute('innerHTML').find('target')-2
                links.append(word_link.get_attribute('innerHTML')[temp_s:temp_e])
                print(' Run complete ')
                # Identify the location 
                didian=li.find_element_by_tag_name('a').text[-3:-1]
                print(didian)
                if didian  != self.place:
                    print(' Wrong address ')
                else:
                    print(' The address is correct ')
                    self.url = "https:" + links[i]
                i=i+1

            self.driver.get(self.url)  #  Load to the purchase interface 
            self.status = 2
            print("### Choose a concert ###")

There's a little dot ,find_element_by_id and find_elements_by_id It's different , The latter needs to add [0].

Again, the next step is on the purchase page , Select ticket price , Time , Then click buy , For the ticket price, we need to judge whether it is available , If you have goods, choose first , The rest are sorted according to the price , If you have time, choose according to your personal flexibility .

    def choose_ticket(self):
        if self.status == 2:
            self.num = 1  #  First attempt 
            time_start = time.time()
            while self.driver.title.find(' Confirmation of order ') == -1:  #  If you jump to the order settlement interface, this one is successful 
                if self.num != 1:  #  If you failed the previous time , Then refresh the interface and start again 
                    self.status = 2
                    self.driver.get(self.url)
                try:
                    element = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.CLASS_NAME,"select_right_list_item")))
                    print(' Pass the verification ')
                except Exception as e:
                    print(e)

                self.driver.find_elements_by_class_name('perform__order__select perform__order__select__performs')
                #datelist= self.driver.find_elements_by_class_name('select_right_list_item')
                print(' Successfully found ')
                datelist=self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div[2]/div[4]/div[3]/div[2]/div')[0].find_elements_by_tag_name('div')
                print(len(datelist))
                #j = datelist[0].get_attribute('span')
                # Judge whether to buy first through ticketing 
                info=self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div[2]/div[4]/div[3]/div[2]/div')[0].find_elements_by_class_name('presell')

                # Judge whether there is a ticket 
                for i in range(len(info)):
                    INFO=info[i].text
                    if INFO == ' There are tickets ':
                        datelist[i].click()
                        break
                    else:
                        datelist[1].click()

                #datelist[2].click()
                sleep(1)
                print(' Click success ')


                #  Select a feasible fare according to priority ,
                #  First buy what is not out of stock , Buy the cheapest 
                pricepalce=self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div[2]/div[4]/div[5]/div[2]/div')[0].find_elements_by_class_name('select_right_list_item')
                pricelist=self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div[2]/div[4]/div[5]/div[2]/div')[0].find_elements_by_class_name('notticket')


                for i in range(len(pricelist)):
                    PH=pricelist[i].text
                    if PH == ' Out of stock registration ':
                        continue
                    else:
                        pricepalce[i].click()
                        break

                print("### Choose concert time and ticket price ###")

                # Click the buy button 
                cart = self.driver.find_elements_by_xpath('/html/body/div[2]/div/div[1]/div[1]/div/div[2]/div[4]')[0]
                sleep(3)
                cart.find_elements_by_class_name('buybtn')[0].click()

                sleep(3)
                self.status = 3
                print(' Jump completed ')
                try:
                    element = WebDriverWait(self.driver, 3).until(EC.title_is(u' Confirmation of order '))
                    print(" Confirm positioning ")
                except:
                    print('### There are no tickets left ###')
                    # close 
                    #self.driver.quit()
            time_end = time.time()
            print("### after %d Round struggle , Total time consuming %f second , Successful ticket grabbing ! Please confirm the order information ###" % (self.num - 1, round(time_end - time_start, 3)))
 Be careful   find_element_by_link_text It seems that we can only locate <a></a> Elements in .

Last , Just on the submit order page , Judge , Then select passengers , Address Click submit order to enter Alipay page. .

    def check_order(self):
        if self.status in [3, 4, 5]:
            print('### Start order confirmation ###')
            self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div[2]/div[1]/div/label/span[1]/input').click()

            print(' Enter this stage ')
            self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[9]/button').click()  #  Agree to the above agreement and submit the order 
            try:
                element = WebDriverWait(self.driver, 5).until(EC.title_contains(' payment '))
                self.status = 6
                print('### Order submitted successfully , Please pay manually ###')
                sleep(100)
            except:
                print('### Failed to submit order , Please check the question ###')

    def finish(self):
        self.driver.quit()

Because Lin Junjie has no tickets , So change it to Zhang Jie during the experiment ... Finally, call the function one by one .

if __name__ == '__main__':
    try:
        # Change the concert information you want to buy here 
        con = Concert(' Zhang jie ', [1], [2], ' Suzhou ', 1)  #  For details, please check the initialization function in the class 
        con.enter_concert()
        con.choose_ticket()
        con.check_order()

    except Exception as e:
        print(e)
        con.finish()
 ————————————— Summary —————————————————

The time for a single ticket purchase is about 15.8s, On the whole, it's embarrassing , I think if you want to actually use , Or should we use js Script simulates the way of operation , Enable directly as a plug-in , such 1. It's not easy to climb back ,2. It saves a lot of unnecessary loading time   The specific operation will be updated later ,

The original code can be found in csdn Download from , But there's no need , It's too messy to tidy up .

I graduated this year , In the second half of the year, I was busy working and didn't have time to write ,6 The contents of the three-year paper will be published slowly after January , Although what I do as a civil engineering student is half hanging stuff , But it should be a good entry toy , Happy new year to you all .

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

版权声明
本文为[Install a sound 77]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551491366.html

随机推荐