Python disk commemorative coin series 3: automatic reservation script writing 01

Time:2020-10-18

We have successfully identified the captcha through neural network. In this issue, we can start to write automatic reservation script.

Page Watch

Taking advantage of the recent reservation of the year of the rat coin, we can use the existing appointment interface to observe:

Python disk commemorative coin series 3: automatic reservation script writing 01

Compared with the previous page, the current page can fill in the number of appointments:

Python disk commemorative coin series 3: automatic reservation script writing 01

Other UI that didn’t respond before also had their own response.

On this basis, we can start writing scripts.

Configuration file design

Since we want to write scripts, we have to do a lot of repetitive work. Here, it is to quickly input multiple groups of information to reserve commemorative coins. Therefore, we need to prepare the required data in a certain format in advance, so that the script can read it automatically when it runs. Here is an example:

users = {
    'name_1': {
        'identNo': 'identNo_1',
        'mobile': 'mobile_1',
        'location': 'location_1',
        'cardvalue0': 'cardvalue0_1'
    },
    'name_2': {
        'identNo': 'identNo_2',
        'mobile': 'mobile_2',
        'location': 'location_2',
        'cardvalue0': 'cardvalue0_2'
    },
    'name_3': {
        'identNo': 'identNo_3',
        'mobile': 'mobile_3',
        'location': 'location_3',
        'cardvalue0': 'cardvalue0_3'
    }
    ...
}

The configuration file contains the name, ID number, mobile phone number, location information and the number of appointments. These are fixed information that can be configured in advance, and the program will automatically fill in the corresponding location after startup.

Appointment scripting

Before writing a formal script, we need to write the verification code identification into an independent function block according to the previous article, so as to avoid code confusion

def recognize_capchar(capchar, model, lb):
    result = ''
    image = cv2.imread(capchar, 0)
    image = image[1:-1, 1:-1]
    target_patches = get_cutted_patches(image)
    for target_patch in target_patches:
        image = cv2.resize(target_patch, (16, 16))
        #Scale image data
        image = image.astype("float") / 255.0
        image = np.expand_dims(image, axis=-1)
        #Flatten the image
        image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
        #Forecast
        preds = model.predict(image)
        #The prediction results and the corresponding labels are obtained
        i = preds.argmax(axis=1)[0]
        label = lb.classes_[i]
        result += label
    return result

Try to fill in the information by yourself, and then click Submit. It is found that there is no API for submitting appointment information directly on this website. Therefore, the best way to do this is to use theseleniumThrough the way of automated testing.

Creating browser objects

In order to demonstrate clearly, we useChromeTo test.

To run this code, you need to installseleniumThe installation method is simple:

pip install selenium

In addition, we need toChromeDrive. Drive you can search and download on the Internet, and the official website needs to surf the Internet scientifically. It should be noted that the download driver should follow theChromeAnd then save the executable file in thePathLocation that can be accessed.

After the preparation is done, it is very simple to create a browser object. You just need to import the corresponding package and create a new object

from selenium import webdriver
driver = webdriver.Chrome()

The results are as follows:

Python disk commemorative coin series 3: automatic reservation script writing 01

Visit the corresponding website

Since the website of each appointment is different, it is necessary to define the website of this appointment in advance. Then use the browser object to access it. The code is still as simple as before

url = 'https://eapply.abchina.com/coin/coin/*******'
driver.get(url)

The results are as follows:

Python disk commemorative coin series 3: automatic reservation script writing 01

Fill in the information

At this stage, there is no difference between the web page and our own visit. We just need to fill the corresponding information into the corresponding grid one by one. Then we will do this next.

First, we need to let the program know where to fill in the next information:

ele_name = driver.find_element_by_xpath('//*[@id="name"]')

this//*[@id="name"]Maybe I don’t know how it came from. It’s very simple. After opening the webpage, first find the small arrow according to the second point in Python disk commemorative coin series: identification verification code 01, then click the corresponding input box, right-click the corresponding position in the source code window on the right, and selectCopy -> Copy XPathYou can get it.

After obtaining the location, it is natural to fill in the information, which is also quite simple

ele_ name.send_ Keys ('name ')

In this way, the “name” can be automatically filled in to the corresponding position, and other fixed information (ID number, mobile phone number
The same is true for the new year’s Coins (the first batch) in 2020.

ToDo

  1. Captcha processing
  2. Drop down box processing
  3. Date processing

Postscript

Limited to space, I’ll just write here first and thenCaptcha processingDrop down box processingDate processingWe will continue to introduce it in the next issue.

All the source code of this series will be placed in the GitHub repository below. If necessary, you can refer to it. If there is any problem, please correct it. Thank you!

https://github.com/TitusWongCN/AutoTokenAppointment

The following will be the last article in this series. The main contents include verification code processing, drop-down box processing and date processing. Please look forward to it.


First issue: Python disk commemorative coin series 1: Introduction

The second issue: Python disk Commemorative Coin Series II: identification verification code 01

Issue 3: Python disk commemorative coin series 2: identification verification code 02

Issue 4: Python disk commemorative coin series 2: identification verification code 03

Issue 5: Python disk commemorative coin series 2: identification verification code 04