醫院預約掛號腳本
功能介紹
本腳本是一個用 Python 編寫的醫院預約掛號程序,支持以下功能:
- 自動預約:通過api交互選擇醫院、科室、醫生和時間段。
- 自動監控:持續檢查指定醫生的號源狀態,發現可預約時段時自動嘗試預約。
核心代碼解析
1. 主程序 (main.py
)
# -*- coding: utf-8 -*-
import time
from schema import AddRegArgs, DeptSchForDocArgs, DocSchArgs, NumberSourceArgs
from api import Apidef appointment():"""預約程序入口,通過命令行交互完成掛號"""api = Api()hospitals = api.get_hospital()print("請選擇院區,輸入院區前方序號")for index, hospital in enumerate(hospitals):print("{}):{}".format(index, hospital["aliasName"]))hospital_index = int(input("請輸入院區序號:"))hospital = hospitals[hospital_index]# 后續科室、醫生、時間選擇邏輯...# 完整代碼見 a.txtdef monitor():"""每10秒檢查一次號源狀態,發現可預約時段時自動嘗試預約"""while True:try:# 檢查號源狀態doc_schDate = api.get_doc_sch(args)if len(doc_schDate) > 0:# 嘗試自動預約response = api.add_reg(args)if response["code"] == "0":print("預約成功!")except Exception as e:print(f"程序異常:{e}")time.sleep(10)if __name__ == "__main__":monitor()
2. 數據模型 (schema.py
)
# -*- coding: utf-8 -*-
from pydantic import BaseModel
from typing import Unionclass HospitalArgs(BaseModel):sysCode: Union[str, int] = "1001035"class DeptListArgs(BaseModel):clinicalType: str = 1hosId: intsysCode: Union[str, int] = "1001035"class DeptSchForDocArgs(BaseModel):clinicalType: str = 1deptCode: str = NonedeptId: str = deptCodespecialtyId: str = NonedeptName: strvisitingArea: strhosId: intrecommendation: str = ""type: str = "order"source: int = 22sysCode: Union[str, int] = "1001035"def update(self):self.deptId = self.deptCodeclass DocSchArgs(BaseModel):deptId: strdeptName: strdocId: strdocName: strdocTitle: strdocPhoto: strhosId: inthosName: strtype: str = "order"visitingArea: str = ""clinicalType: str = 1source: int = 22sysCode: Union[str, int] = "1001035"class NumberSourceArgs(BaseModel):sysCode: Union[str, int] = "1001035"ampm: strcategor: strdocId: strdeptId: strhosId: Union[str, int]schDate: strschId: strclass AddRegArgs(BaseModel):# key不一致參數disNo: str = NoneschDate: str = NonepatienId: str = None# 必須參數clinicalType: str = 1visitingArea: str = ""ampm: strappointmentNumber: str = disNocategor: strcategorName: strdeptId: strdeptName: strdocId: strdocName: strendTime: str = ""extend: str = ""fee: strhosId: Union[str, int]hosName: strisFlexible: str = ""numId: str = ""patientId: str = patienIdresDate: str = schDateschId: strsource: int = 22startTime: str = ""sysCode: Union[str, int] = "1001035"thirdUserId: str = ""timeDesc: strtimePoint: str = ""schQukCategor: strdef update(self):self.appointmentNumber = self.disNoself.resDate = self.schDateself.patientId = self.patienId
3. 配置文件 (conf.ini
)
[**省中醫院]
host = https://www.rjh.com.cn/
deskey = wewerwe
username = 你的賬號
password = 你的密碼
token = token
使用說明
-
運行環境:
- Python 3.6+
- 依賴庫:
pydantic
、requests
-
啟動方式:
python main.py
-
配置說明:
- 修改
conf.ini
中的賬號和醫院配置 - 支持直接使用 token 登錄
- 修改
實現細節
-
字典展開語法:
all_args = {**dept, **hospital} args = DeptSchForDocArgs(**all_args)
-
異常處理:
- 捕獲網絡請求異常
- 支持 Ctrl+C 中斷程序
-
定時檢測:
- 每10秒檢測一次號源
- 預約成功后等待10分鐘
提示:部分代碼,已脫敏,本腳本僅用于學習交流,請勿濫用。歡迎感興趣的同學一起交流!