医院预约挂号
医院预约挂号脚本
功能介绍
本脚本是一个用 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分钟
提示:部分代码,已脱敏,本脚本仅用于学习交流,请勿滥用。欢迎感兴趣的同学一起交流!