# we-mp-rss 正文提取参考

## 数据库结构

```
articles 表关键字段:
- content: TEXT — 完整HTML正文（15KB-2MB/篇）
- description: TEXT — 摘要（通常4-14字，如"点击查看！"）
- has_content: INTEGER — 是否已抓取正文（1=是）
- status: INTEGER — 文章状态
```

## DB 路径

生产库：`/home/ubuntu/project/we-mp-rss-data/db.db`（37MB，117篇）
废弃旧库：`/home/ubuntu/project/we-mp-rss-main/data/db.db`（229KB）

## 查询示例

```python
import sqlite3
db = sqlite3.connect('/home/ubuntu/project/we-mp-rss-data/db.db')
db.row_factory = sqlite3.Row

# 查询最近48小时文章（含正文）
cutoff = int((datetime.now(CST) - timedelta(hours=48)).timestamp())
rows = db.execute("""
    SELECT a.title, a.url, a.publish_time, f.mp_name as source, 
           a.description, a.content
    FROM articles a JOIN feeds f ON a.mp_id = f.id
    WHERE f.mp_name IN ('广东中职菌', '中职生')
      AND a.publish_time >= ?
    ORDER BY a.publish_time DESC LIMIT 30
""", [cutoff]).fetchall()
```

## _html_to_text 提取函数

当前实现（`~/project/scripts/daily_xhs_topics.py`）：

```python
def _html_to_text(html):
    """HTML→纯文本，去脚本/样式/标签"""
    import re as _re
    if not html: return ""
    text = str(html)
    # 1. 去掉 <script> 和 <style> 块
    text = _re.sub(r'<(script|style)[^>]*>.*?</\1>', ' ', text, 
                   flags=_re.DOTALL | _re.IGNORECASE)
    # 2. 去掉 HTML 注释
    text = _re.sub(r'<!--.*?-->', ' ', text, flags=_re.DOTALL)
    # 3. 去掉 HTML 标签
    text = _re.sub(r'<[^>]+>', ' ', text)
    # 4. 解码常见实体
    text = _re.sub(r'&nbsp;|&lt;|&gt;|&amp;|&quot;|&#?\w+;', ' ', text)
    # 5. 合并空白，去首尾
    text = _re.sub(r'\s+', ' ', text).strip()
    return text
```

## 有效提取率

约 50% 文章可提取有效正文（1700-4500字）。其余为微信界面残渣（需 JS 渲染）。

**过滤规则：**
```python
text = _html_to_text(raw)
if text and len(text) > 200 and '微信扫一扫' not in text[:100]:
    # 有效正文
    snippet = text[:500]  # 截取前500字注入 prompt
```

## we-mp-rss 配置

`config.yaml`:
```yaml
gather:
  content: True           # 新文章同步抓取全文
  content_auto_check: True  # 每59分钟补抓遗漏
  browser_type: firefox     # 完整渲染含图片
```

DB 环境变量（systemd service）：
```
DB=sqlite:////home/ubuntu/project/we-mp-rss-data/db.db
```

## 历史：为什么之前以为正文不可用

1. pipeline `daily_xhs_topics.py` 的 `DB_PATH` 指向了错误的旧库（229KB）
2. pipeline 只读 `description` 字段（4-14字摘要），完全忽略 `content`
3. `_html_to_text` 原始版本不去 `<script>` 标签，提取结果含 JS 代码
4. 技能文档在 2026-06-25 基于旧 pipeline 行为写了「正文为空」，2026-06-26 已修正
