程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+selenium to realize automatic hospital registration

編輯:Python

Python+selenium Realize automatic hospital registration

In some hospitals, the names of some experts are always “ Number one is hard to find ”, Basically, the ticket is empty , In this case , A small partner who has mastered certain technologies can register using automated methods

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

Technology used :

  • python 3.7
  • selenium library
  • xpath

You need to log in before grabbing the number

Prepare the identity information and web page for login url, There is a verification code for the login of the web page , Select the way to automatically open the browser so that you can observe the verification code and enter it manually , And set up 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 target doctor's url, And simulate the normal registration of the 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 registration operation is as follows

First, jump to the doctor interface

The beginning shows Thursday 02-17 There are signs , Check the web page source code and find that the element is a span Type element , Click to execute getsurplus Method to obtain the time period available for reservation , and 435255 It's sending post Request the parameters that need to be passed

So the first step is to click on this span Elements , Notice here that you can find the element directly and execute click() You can't , Click only and you will not be able to execute gesurplus, Must be followed by perform()

 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, The parameter is the registration 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 the ticket was released at a fixed time , It needs to be constantly refreshed , So put the above code in a loop , Execute once at regular intervals , And output information , If there is no ticket, refresh , Without ticket , Corresponding span Of class The attribute is disabled, With this point, functions can be realized

 while True:
# If the element cannot be clicked, then 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(" The appointment was successful ")
break
else:
time.sleep(0.1)
print(" It is 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, then 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(" The appointment was successful ")
break
except:
print(" It's already hung up , Continue to try to ")
time.sleep(0.1)
browser.refresh()
continue
else:
time.sleep(0.1)
print(" It is not time to release tickets ")
browser.refresh()
# If the element is successfully clicked, the success message will be output and the program will exit 
# If it is already engaged, output the information and exit 
if __name__ == '__main__':
login("xxx", "xxx")

Running results


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved