import re import json with open('/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/Đào tạo tại UMC.html', 'r', encoding='utf-8') as f: html = f.read() parts = html.split('
') groups = [] slugs = ['gallery', 'chung-chi', 'giay-chung-nhan', 'xac-nhan-thuc-hanh', 'hinh-anh-thuc-hanh'] names = ['Gallery Đào Tạo', 'Đào tạo cấp chứng chỉ', 'Đào tạo cấp giấy chứng nhận', 'Xác nhận quá trình thực hành', 'Hình ảnh thực hành'] for i in range(1, len(parts)): part = parts[i] slug = slugs[i-1] if (i-1) < len(slugs) else f"unknown-{i}" name = names[i-1] if (i-1) < len(names) else f"Unknown {i}" # We only care about the slides for this swiper. # The swiper ends with `
tags inside `swiper-slide` that appear before the next wrapper. # Actually, we can split by `swiper-slide` slides_html = part.split('class="swiper-slide') items = [] for j in range(1, len(slides_html)): slide = slides_html[j] # find image img_match = re.search(r']*src="([^"]+)"', slide) img_url = img_match.group(1) if img_match else "" # find title - it's usually inside or just alt text. # Let's check heading-5 title_match = re.search(r']*>(.*?)', slide, re.IGNORECASE | re.DOTALL) if title_match: title = re.sub(r'<[^>]+>', '', title_match.group(1)).strip() else: alt_match = re.search(r']*alt="([^"]*)"', slide) title = alt_match.group(1) if alt_match else "" items.append({ "title": title, "imageUrl": img_url, "linkUrl": "#", "description": "" }) # Filter empty items = [it for it in items if it['imageUrl']] groups.append({ "slug": slug, "name": name, "items": items }) print(json.dumps(groups, ensure_ascii=False))