import re with open('src/main/resources/templates/doctor.html', 'r', encoding='utf-8') as f: html = f.read() # 1. Extract all unique specialties specialties = set() matches = re.finditer(r'
(.*?)
', html) for m in matches: spec = m.group(1).strip() if spec and spec != "BS CKII." and spec != "TS BS." and spec != "ThS.": # Actually, let's be careful. The title (BS CKII) has the same class! # Let's just look at the ones that start with "Khoa" or "Đơn vị" or just filter titles if not spec.startswith("BS ") and not spec.startswith("TS ") and not spec.startswith("ThS.") and not spec.startswith("PGS ") and not spec.startswith("GS "): specialties.add(spec) specialties = sorted(list(specialties)) print(f"Found {len(specialties)} specialties.") # 2. Add IDs to the buttons html = html.replace( 'aria-haspopup="listbox" aria-expanded="false">--Theo chuyên khoa--', 'id="btn-specialty" aria-haspopup="listbox" aria-expanded="false" onclick="toggleDropdown(\'list-specialty\')">--Theo chuyên khoa--' ) html = html.replace( 'aria-haspopup="listbox" aria-expanded="false">--Theo giới tính--', 'id="btn-gender" aria-haspopup="listbox" aria-expanded="false" onclick="toggleDropdown(\'list-gender\')">--Theo giới tính--' ) # 3. Add IDs to the search input html = re.sub( r'--Tất cả chuyên khoa--' for spec in specialties: spec_list_html += f'
  • {spec}
  • ' spec_list_html += '' gender_list_html = '' # 5. Inject the lists after the buttons # Find the div containing the specialty button spec_btn_end = html.find('', html.find('id="btn-specialty"')) + 9 html = html[:spec_btn_end] + spec_list_html + html[spec_btn_end:] gender_btn_end = html.find('', html.find('id="btn-gender"')) + 9 html = html[:gender_btn_end] + gender_list_html + html[gender_btn_end:] # Add an ID to the doctor cards container html = html.replace( '
    ', '
    ' ) # 6. Inject the JavaScript at the end of the file js_code = """ """ html = html.replace('
    \n', js_code + '\n
    \n') with open('src/main/resources/templates/doctor.html', 'w', encoding='utf-8') as f: f.write(html) print("Injected JS and dropdown HTML")