#!/usr/bin/env python3 """ 在章节标题(h2)前插入分隔线,第一个除外 """ import re HTML_PATH = "/root/openclaw-workspace/projects/yu-zhi-ran/content/published/2026-04-14-上海阳台种菜一年/article-optimized.html" with open(HTML_PATH, "r", encoding="utf-8") as f: html = f.read() # 分隔线HTML separator = '
\n' # 找到所有 h2 标题 h2_pattern = re.compile(r'(

.*?

)', re.DOTALL) matches = list(h2_pattern.finditer(html)) # 跳过第一个 h2,对其余每个插入分隔 insertions = [] for i, m in enumerate(matches[1:], start=1): # 从第二个开始 insert_pos = m.start() insertions.append((insert_pos, separator)) # 按位置逆序插入,避免影响后续位置 insertions.sort(reverse=True, key=lambda x: x[0]) html_list = list(html) for pos, sep in insertions: html_list.insert(pos, sep) new_html = ''.join(html_list) # 写回 with open(HTML_PATH, "w", encoding="utf-8") as f: f.write(new_html) print(f"✅ 已插入 {len(insertions)} 个章节分隔") print(f"📄 文件: {HTML_PATH}")