from requests_html import HTML from lxml import html as lxml_html class TTSHTMLParser: html: HTML def __init__(self, html: str): # keep original string and also parse with lxml for reliable element API self.raw_html = html self.html = HTML(html=html) self._root = lxml_html.fromstring(html) # 解析为[音色,文本]的元组列表 # 从最里层节点遍历获取 def parse( self, strip: bool = True, delete_eol: bool = True, delete_space: bool = True, strict: bool = False, # 只保留字母,数字,中文和部分常用标点 ) -> tuple[str, str]: result = [] def clean(s: str) -> str: if s is None: return "" t = s if not strip else s.strip() if delete_space: t = t.replace(" ", "") if strict: allowed_punc = { ",", ".", ";", "-", "—", "-", "–", ",", "。", ";", "、", "!", "?", "!", "?", "%", "%", "+", "+", } def is_chinese(ch: str) -> bool: return "\u4e00" <= ch <= "\u9fff" t = "".join( c for c in t if c.isalnum() or is_chinese(c) or c in allowed_punc ) return t.replace("\n", ",") if delete_eol else t def process(elem) -> None: if elem.tag == "root": for child in elem: process(child) return texts = elem.xpath("text()") before_raw = texts[0] if texts else "" before = clean(before_raw) if before != "": result.append((elem.tag, before)) children = list(elem) if not children: return for child in children: process(child) after_raw = "".join(texts[1:]) if len(texts) > 1 else "" after = clean(after_raw) if after != "": result.append((elem.tag, after)) root = self._root process(root) return tuple(result)