import sys from bs4 import BeautifulSoup import re import unicodedata def slugify(value): value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('utf-8') value = re.sub(r'[^\w\s-]', '', value).strip().lower() return re.sub(r'[-\s]+', '-', value) # The slugs used in our DB db_slugs = [ "chuyen-khoa-tai-mui-hong", "khoa-chan-doan-hinh-anh", "khoa-cap-cuu", "khoa-dinh-duong-tiet-che", "khoa-duoc", "khoa-kham-benh", "don-vi-kiem-soat-nhiem-khuan", "khoa-ngoai-tong-hop", "tim-mach", # This is Khoa Nội Tổng Hợp in our DB "phau-thuat-gay-me-hoi-suc", "khoa-than-kinh-dot-quy", "khoa-vat-ly-tri-lieu-phuc-hoi-chuc-nang", "khoa-xet-nghiem", "phong-quan-ly-van-hanh", "don-vi-can-thiep-mach-dsa", "don-vi-cap-cuu-ngoai-vien", "don-vi-kham-suc-khoe-ngoai-vien" ] manual_mapping = { "noi tong hop": "tim-mach", "phau thuat gay me hoi suc phong mo": "phau-thuat-gay-me-hoi-suc", "dsa": "don-vi-can-thiep-mach-dsa", "tai mui hong": "chuyen-khoa-tai-mui-hong", "kiem soat nhiem khuan": "don-vi-kiem-soat-nhiem-khuan", "kham suc khoe ngoai vien": "don-vi-kham-suc-khoe-ngoai-vien", "cap cuu ngoai vien": "don-vi-cap-cuu-ngoai-vien" } html_path = '/home/x79/sisvietnamvn_01/BV_DHYD_HCM/Các chuyên khoa tại Bệnh viện Đại học Y Dược TP. Hồ Chí Minh.html' with open(html_path, 'r', encoding='utf-8') as f: soup = BeautifulSoup(f, 'html.parser') icons = {} links = soup.find_all('a', href=True) for a in links: href = a.get('href') if '/chuyen-khoa/' in href: img = a.find('img') if img: src = img.get('src') if not src: # Next image srcset or similar fallback src = img.get('data-src') or img.get('srcset') if src and ' ' in src: src = src.split(' ')[0] title = href.split('/')[-1] # use the slug from href # Find the actual name name_span = a.find('span') if name_span: title = name_span.text.strip() else: title_text = a.text.strip() if title_text: title = title_text if src: # Decode src if it's encoded or make it absolute if src.startswith('/_next/image?url='): import urllib.parse src = urllib.parse.unquote(src.replace('/_next/image?url=', '').split('&')[0]) # Match it s_text = slugify(title) matched_slug = None for key, val in manual_mapping.items(): if slugify(key) in s_text: matched_slug = val break if not matched_slug: for s in db_slugs: if s_text in s or s in s_text or s_text.replace("khoa-", "") in s: matched_slug = s break if matched_slug: icons[matched_slug] = src print(f"Mapped {len(icons)} image icons to specialties") # Generate the thymeleaf switch block for images switch_code = '\n' for slug, src in icons.items(): switch_code += f' \n' switch_code += f' \n' switch_code += f' Icon\n' switch_code += f' \n' # Default case switch_code += ' \n' switch_code += ' \n' switch_code += '
\n' switch_code += ' \n' switch_code += ' \n' switch_code += ' \n' switch_code += '
\n' switch_code += '
\n' switch_code += '
' with open('/tmp/icons_block_img.txt', 'w', encoding='utf-8') as f: f.write(switch_code) print("Wrote switch block to /tmp/icons_block_img.txt")