医院预约挂号脚本
功能介绍
本脚本是一个用 Python 编写的医院预约挂号程序,支持以下功能:
- 自动预约:通过api交互选择医院、科室、医生和时间段。
- 自动监控:持续检查指定医生的号源状态,发现可预约时段时自动尝试预约。
核心代码解析
1. 主程序 (main.py
)
# -*- coding: utf-8 -*-
import time
from schema import AddRegArgs, DeptSchForDocArgs, DocSchArgs, NumberSourceArgs
from api import Api
def 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.txt
def 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 Union
class HospitalArgs(BaseModel):
sysCode: Union[str, int] = "1001035"
class DeptListArgs(BaseModel):
clinicalType: str = 1
hosId: int
sysCode: Union[str, int] = "1001035"
class DeptSchForDocArgs(BaseModel):
clinicalType: str = 1
deptCode: str = None
deptId: str = deptCode
specialtyId: str = None
deptName: str
visitingArea: str
hosId: int
recommendation: str = ""
type: str = "order"
source: int = 22
sysCode: Union[str, int] = "1001035"
def update(self):
self.deptId = self.deptCode
class DocSchArgs(BaseModel):
deptId: str
deptName: str
docId: str
docName: str
docTitle: str
docPhoto: str
hosId: int
hosName: str
type: str = "order"
visitingArea: str = ""
clinicalType: str = 1
source: int = 22
sysCode: Union[str, int] = "1001035"
class NumberSourceArgs(BaseModel):
sysCode: Union[str, int] = "1001035"
ampm: str
categor: str
docId: str
deptId: str
hosId: Union[str, int]
schDate: str
schId: str
class AddRegArgs(BaseModel):
# key不一致参数
disNo: str = None
schDate: str = None
patienId: str = None
# 必须参数
clinicalType: str = 1
visitingArea: str = ""
ampm: str
appointmentNumber: str = disNo
categor: str
categorName: str
deptId: str
deptName: str
docId: str
docName: str
endTime: str = ""
extend: str = ""
fee: str
hosId: Union[str, int]
hosName: str
isFlexible: str = ""
numId: str = ""
patientId: str = patienId
resDate: str = schDate
schId: str
source: int = 22
startTime: str = ""
sysCode: Union[str, int] = "1001035"
thirdUserId: str = ""
timeDesc: str
timePoint: str = ""
schQukCategor: str
def update(self):
self.appointmentNumber = self.disNo
self.resDate = self.schDate
self.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分钟
提示:部分代码,已脱敏,本脚本仅用于学习交流,请勿滥用。欢迎感兴趣的同学一起交流!