import sys, os from html.parser import HTMLParser CACHE = r"D:\code\crm-需求梳理\lanhu-mcp\data\axure_extract_bade4454" class T(HTMLParser): def __init__(self): super().__init__(); self.out=[]; self.skip=0 def handle_starttag(self,t,a): if t in ('script','style'): self.skip+=1 def handle_endtag(self,t): if t in ('script','style') and self.skip>0: self.skip-=1 def handle_data(self,d): if self.skip: return d=d.strip() if d: self.out.append(d) def extract(fn): p=T() with open(os.path.join(CACHE,fn),encoding='utf-8',errors='replace') as f: p.feed(f.read()) lines=[l for l in ('\n'.join(p.out)).splitlines() if l.strip()] # dedupe consecutive res=[] for l in lines: if not res or res[-1]!=l: res.append(l) return res if __name__=='__main__': files=sys.argv[1:] for fn in files: lines=extract(fn) out=os.path.join(r"D:\code\crm-backend-matt\.scratch\lanhu-text", fn.replace('.html','.txt')) os.makedirs(os.path.dirname(out),exist_ok=True) with open(out,'w',encoding='utf-8') as f: f.write('\n'.join(lines)) print(f"{fn}: {len(lines)} lines -> {out}")