Files
MediaCrawler/mac-agent-os-main/03_knowledge/99_system/matrix/douyin-comment-automation.md
T
butubb 2112c1a870
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s
feat(monitor): 抖音 Web 接口客户端 —— 绕开爬虫子进程,直接发 HTTP
移植自 mac-agent-os 的 mediacrawler_adapter:不起子进程、不开页面,用浏览器里那份
登录态直接调抖音 Web 接口。产物键名照抄 store/douyin,所以 ingest 那条链路一个字不用改。

**目前能用的(真环境实测,非推断)**:

    profile/other : 200, 7075 字节  —— 博主主页指标(粉丝/获赞/作品数/昵称)
    aweme/detail  : 200, 45425 字节 —— 单条作品详情(含点赞/评论/收藏/分享)

**目前不能用的:作品列表 `aweme/post`。** 两个互相独立的原因:
  1. 这个接口被抖音单独升级成了真校验:不带 x-tt-argus 回 403「Uifid Not Found」,
     带上 dummy 值回 200 + **空 body**。也就是说「头在不在」骗得过,「真校验」过不了。
     同一套头打 profile/other 和 aweme/detail 都是通的 —— 抖音是挑着接口加保护的,
     挑中的恰好是「批量拉作品列表」这个最敏感的动作。
  2. 改走页面截获也不行:CDP 浏览器打开博主主页会落到「验证码中间页」(当天大量探测的
     代价,过几小时要重测)。

  所以现在的边界是:**已知作品的指标刷新能做,自动发现新作品做不了**。

**排查中控住变量后得到的两条事实**(都写进注释了):
  · `Accept` / `Accept-Language` / `Referer` 才是主页接口能返回真数据的原因 —— 只有
    UA+client hints+Cookie 时是 200 但仅 121 字节的空壳,补上这三个头变 7074 字节。
    (我先前猜的 sec-ch-ua 不是关键。)
  · 因此 UA 与 client hints 必须**成套地取自同一个浏览器**,所以 BrowserIdentity 一次
    从 CDP 取齐 cookie + UA + hints,而不是各自写死。

「200 + 空 body 必须当场报错」也是刻意写死的:放过去它会在下游变成「这个博主没作品」,
把一次失败伪装成一条正常结果 —— 爬虫那条路正是这么栽的,还被翻译成「账号被封」。

测试 +11:cookie 解析、请求头成套性(含 uifid 缺失/回退)、产物键名与 store 对齐、
以及 _get 的三条失败路径(空 body / 403 带网关原话 / 正常返回)。
2026-10-10 17:12:25 +08:00

130 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 抖音评论自动化双路径技术
tags: [douyin, comment, automation, draft.js, keyboard]
created: 2026-05-29
updated: 2026-05-29
nature: method
collected: true
collected_date: 2026-06-09
---
## 概述
抖音评论系统基于 Draft.js 富文本编辑器,具有严格的输入验证。通过两条独立路径实现评论自动化。
## 双路径架构
| 路径 | 场景 | 入口操作 | 发送操作 |
|:-----|:-----|:---------|:---------|
| **Path A**: 弹窗覆盖层 | 养号流程中 | KeyX 打开评论区 | Enter 发送 |
| **Path B**: 全屏视频页 | 指定视频链接 | scrollIntoView 定位输入框 | Enter 发送 |
## 输入方式
**唯一可靠方式**:`pbcopy` + `Meta+V`(系统剪贴板粘贴)
Draft.js 富文本编辑器特点:
- 只认系统级键盘事件
- CDP(Chrome DevTools Protocol)注入的键盘事件一律忽略
- `page.fill()` / `page.type()` / `elementHandle.type()` 均不可靠
- 必须通过剪贴板 + 系统快捷键触发输入
```python
# 标准输入流程
import subprocess
subprocess.run(["pbcopy"], input=comment_text.encode(), check=True)
await page.keyboard.press("Meta+v") # 或 Cmd+V
await asyncio.sleep(0.3) # 等待粘贴完成
await page.keyboard.press("Enter") # 发送
```
## 状态机
```
closed → panel_open → input_focused → text_entered → sent / verify_code
```
| 状态 | 说明 | 验证方法 |
|------|------|----------|
| closed | 评论区关闭 | 评论面板不可见 |
| panel_open | 评论区已打开 | 评论列表 DOM 可见 |
| input_focused | 输入框已聚焦 | `document.activeElement` 指向编辑器 |
| text_entered | 文字已粘贴 | 输入框内容非空 |
| sent | 发送成功 | 新评论出现在列表顶部 |
| verify_code | 触发验证码 | 验证码弹窗出现,需人工处理 |
## Draft.js 空格刷新技巧
粘贴文本后,Draft.js 可能不立即更新 React 状态。解决方法:
```python
# 粘贴后按空格刷新 React 状态
await page.keyboard.press("Space")
await asyncio.sleep(0.1)
await page.keyboard.press("Backspace") # 删除多余空格
```
## 视频页评论区定位(Path B 专项)
**问题**:评论区输入框可能在视口外(y ~2212px)
**解决**:
1. `scrollIntoView()` 将输入框滚动到可视区域
2. 等待 DraftEditor 懒加载初始化(关键步骤,不能跳过)
3. 点击容器元素触发编辑器激活
4. 然后才能聚焦、粘贴
```python
input_container = await page.query_selector("评论输入框容器选择器")
await input_container.scroll_into_view_if_needed()
await asyncio.sleep(0.5) # 等待 DraftEditor 懒加载
await input_container.click() # 触发编辑器激活
await asyncio.sleep(0.3)
# 现在可以粘贴了
```
## 抖音快捷键参考
| 快捷键 | 功能 |
|--------|------|
| KeyZ | 点赞 |
| KeyX | 打开/关闭评论 |
| KeyG | 关注 |
| KeyP | 连播切换 |
| KeyM | 静音切换 |
| KeyJ | 清屏/弹幕切换 |
## 评论内容生成
### AI 评论(可选)
```python
from matrix_modules.comment.ai_generator import AICommentGenerator
ig = AICommentGenerator()
note_info = await page.evaluate("""
() => {
const t = document.querySelector('.title, h1');
const c = document.querySelector('.content, .desc');
return { title: t?.textContent?.trim(), content: c?.textContent?.trim() };
}
""")
comment_text = ig.generate_with_context(
note_title=note_info.get("title", ""),
note_content=note_info.get("content", ""),
)
```
### 随机评论池
预置评论列表,`random.choice()` 选取。评论频率建议每 3 轮 1 次,避免被检测。
## 错误处理
| 错误 | 原因 | 处理 |
|------|------|------|
| 粘贴无效 | 系统剪贴板被占用 | 重试 pbcopy |
| Enter 无响应 | 输入框未聚焦 | scrollIntoView + click 重新聚焦 |
| 验证码弹窗 | 频繁操作触发 | 截图保存,跳过本轮 |
| 评论发送失败 | 网络或限流 | 等待后重试,计入 consecutive_failures |