Python+selenium to realize automatic hospital registration

Python+selenium to realize automatic hospital registration

In some hospitals, some experts Daniel's number is always "No. 1 is difficult to find", and the ticket is basically empty for seconds. In view of this situation, small partners who master certain technology can register using automatic methods

First, declare that the script is only used for entertainment and related technical learning, not for commercial purposes

Technology used:

  • python 3.7
  • selenium Library
  • xpath

You need to log in before grabbing the number

Prepare the identity information for login and the url of the web page. Since the login of the web page has a verification code, select the way to automatically open the browser to facilitate the observation of the verification code and manual input, and set the browser implicitly_ Wait (10) to wait for the element to load

def login(username, password):

    url = "http://wxyy.nxtcm.com/cmph-web/login"
    browser = webdriver.Chrome(executable_path=(
    	r'D:\Software\Google\Chrome\Application\chromedriver.exe'))
    browser.implicitly_wait(10)
    browser.get(url)

    browser.find_element(By.ID, "username").send_keys(username)
    browser.find_element(By.ID, "password").send_keys(password)
    code = input('Please enter the verification code:')

    browser.find_element(By.ID, "validateCode").send_keys(code)

    browser.find_element(By.ID, "btn-login").submit()
    

After successful login, jump to the url of the target doctor and simulate the normal registered mouse operation

	target_url = "http://wxyy.nxtcm.com/cmph-web/interhos/clinic/reg/order?doc_id=0923705b-a14f-11ea-b0d0-39cb51340fae"
    browser.get(target_url)

The normal operation of registration is as follows

First, jump to the doctor interface

At the beginning, the number of Thursday 02-17 is displayed. Check the web page source code and find that the element is a span type element. After clicking, the getsurplus method will be executed to obtain the time period available for reservation, and 435255 is the parameter to be passed when sending the post request

Therefore, the first step is to click the span element. Here, it should be noted that it is not possible to directly find the element and execute click(). Only clicking can not execute gesurplus, and perform() must be executed later

    time1=browser.find_element(By.XPATH, "//td[@id='2022-02-17am']/span")
    ActionChains(browser).move_to_element(time1).click(time1).perform()

Then select the time period and confirm the submission

Submission is also a post request suborder, whose parameter is the registered information

    browser.find_element(By.XPATH,".//div[@class='head']/ul/li[1]").click()
   browser.find_element(By.XPATH,"//div[@class='money_button']/a[@id='orderbtn']").click() 

Then, because the ticket is released at a fixed time and needs to be refreshed continuously, put the above code in a cycle, execute it at a certain interval, and output the information. If there is no ticket, refresh it. When there is no ticket, the class attribute of the corresponding span is disabled. This can be used to realize the function

    while True:

        # If the element cannot be clicked, continue
        status = browser.find_element(
            By.XPATH, "//td[@id='2022-02-22am']/span").get_attribute("class")
        if status != "disabled":
            time1 = browser.find_element(
                By.XPATH, "//td[@id='2022-02-22am']/span")
            time1.click()
            print(time1)
            ActionChains(browser).move_to_element(
                time1).click(time1).perform()
            browser.find_element(
                By.XPATH, ".//div[@class='head']/ul/li[1]").click()
            browser.find_element(
                By.XPATH, "//div[@class='money_button']/a[@id='orderbtn']").click()

            print("Appointment succeeded")
            break

        else:
            time.sleep(0.1)
            print("It's not time to release tickets")
            browser.refresh()

The complete code is as follows

# coding = utf-8

import time
from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


def login(username, password):
    url = "http://wxyy.nxtcm.com/cmph-web/login"
    browser = webdriver.Chrome(executable_path=(
        r'D:\Software\Google\Chrome\Application\chromedriver.exe'))
    browser.implicitly_wait(10)
    browser.get(url)
    browser.find_element(By.ID, "username").send_keys(username)
    browser.find_element(By.ID, "password").send_keys(password)
    code = input('Please enter the verification code:')
    browser.find_element(By.ID, "validateCode").send_keys(code)
    browser.find_element(By.ID, "btn-login").submit()
    
    target_url = "http://wxyy.nxtcm.com/cmph-web/interhos/clinic/reg/order?doc_id=0923705b-a14f-11ea-b0d0-39cb51340fae"
    browser.get(target_url)
    
    date = "2022-02-17am"
#     date="2022-02-22am"
    while True:
        # If the element cannot be clicked, continue
        xpath = '//td[@id="'+date+'"]/span'
        print(xpath)
        status = browser.find_element(
            By.XPATH, xpath).get_attribute("class")
        if status != "disabled":
            try:
                time1 = browser.find_element(
                    By.XPATH, xpath)
                time1.click()
                print(time1.get_attribute("class onclick"))
                ActionChains(browser).move_to_element(
                    time1).click(time1).perform()
                browser.find_element(
                    By.XPATH, ".//div[@class='head']/ul/li[1]").click()
                browser.find_element(
                    By.XPATH, "//div[@class='money_button']/a[@id='orderbtn']").click()
                print("Appointment succeeded")
                break
            except:
                print("It's already hung up. Keep trying")
                time.sleep(0.1)
                browser.refresh()
                continue
        else:
            time.sleep(0.1)
            print("It's not time to release tickets")
            browser.refresh()
        # If the element is clicked successfully, the success message is output and the program exits
        # If you have made an appointment, output the information and exit
if __name__ == '__main__':
    login("xxx", "xxx")

Operation results

Keywords: Python Selenium crawler chrome

Added by matanoosh on Mon, 14 Feb 2022 16:27:58 +0200