comment.py
6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""评论操作,对应 Go xiaohongshu/comment_feed.go。"""
from __future__ import annotations
import logging
import time
from .cdp import Page
from .feed_detail import _check_end_container, _check_page_accessible, _get_comment_count
from .selectors import (
COMMENT_INPUT_FIELD,
COMMENT_INPUT_TRIGGER,
COMMENT_SUBMIT_BUTTON,
PARENT_COMMENT,
REPLY_BUTTON,
)
from .urls import make_feed_detail_url
logger = logging.getLogger(__name__)
def post_comment(page: Page, feed_id: str, xsec_token: str, content: str) -> None:
"""发表评论到 Feed。
Args:
page: CDP 页面对象。
feed_id: Feed ID。
xsec_token: xsec_token。
content: 评论内容。
Raises:
RuntimeError: 评论失败。
"""
url = make_feed_detail_url(feed_id, xsec_token)
logger.info("打开 feed 详情页: %s", url)
page.navigate(url)
page.wait_for_load()
page.wait_dom_stable()
time.sleep(1)
_check_page_accessible(page)
# 点击评论输入触发区域
if not page.has_element(COMMENT_INPUT_TRIGGER):
raise RuntimeError("未找到评论输入框,该帖子可能不支持评论或网页端不可访问")
page.click_element(COMMENT_INPUT_TRIGGER)
time.sleep(0.5)
# 输入评论内容
page.wait_for_element(COMMENT_INPUT_FIELD, timeout=5)
page.evaluate(
f"""
(() => {{
const el = document.querySelector({_js_str(COMMENT_INPUT_FIELD)});
if (el) {{
el.focus();
el.textContent = {_js_str(content)};
el.dispatchEvent(new Event('input', {{bubbles: true}}));
}}
}})()
"""
)
time.sleep(1)
# 点击提交
page.click_element(COMMENT_SUBMIT_BUTTON)
time.sleep(1)
logger.info("评论发送成功: feed=%s", feed_id)
def reply_comment(
page: Page,
feed_id: str,
xsec_token: str,
content: str,
comment_id: str = "",
user_id: str = "",
) -> None:
"""回复指定评论。
通过 comment_id 或 user_id 定位评论,然后回复。
Args:
page: CDP 页面对象。
feed_id: Feed ID。
xsec_token: xsec_token。
content: 回复内容。
comment_id: 评论 ID(优先使用)。
user_id: 用户 ID(备选)。
Raises:
RuntimeError: 回复失败。
"""
if not comment_id and not user_id:
raise ValueError("comment_id 和 user_id 至少提供一个")
url = make_feed_detail_url(feed_id, xsec_token)
logger.info("打开 feed 详情页进行回复: %s", url)
page.navigate(url)
page.wait_for_load()
page.wait_dom_stable()
time.sleep(1)
_check_page_accessible(page)
time.sleep(2)
# 查找目标评论
comment_found = _find_and_scroll_to_comment(page, comment_id, user_id)
if not comment_found:
raise RuntimeError(f"未找到评论 (commentID: {comment_id}, userID: {user_id})")
time.sleep(1)
# 点击回复按钮
reply_selector = f"#comment-{comment_id} {REPLY_BUTTON}" if comment_id else REPLY_BUTTON
page.click_element(reply_selector)
time.sleep(1)
# 输入回复内容
page.wait_for_element(COMMENT_INPUT_FIELD, timeout=5)
page.evaluate(
f"""
(() => {{
const el = document.querySelector({_js_str(COMMENT_INPUT_FIELD)});
if (el) {{
el.focus();
el.textContent = {_js_str(content)};
el.dispatchEvent(new Event('input', {{bubbles: true}}));
}}
}})()
"""
)
time.sleep(0.5)
# 点击提交
page.click_element(COMMENT_SUBMIT_BUTTON)
time.sleep(2)
logger.info("回复评论成功")
def _find_and_scroll_to_comment(
page: Page,
comment_id: str,
user_id: str,
max_attempts: int = 100,
) -> bool:
"""查找并滚动到目标评论。"""
logger.info("开始查找评论 - commentID: %s, userID: %s", comment_id, user_id)
# 先滚动到评论区
page.scroll_element_into_view(".comments-container")
time.sleep(1)
last_count = 0
stagnant = 0
for attempt in range(max_attempts):
# 检查是否到底
if _check_end_container(page):
logger.info("已到达评论底部,未找到目标评论")
break
# 停滞检测
current_count = _get_comment_count(page)
if current_count != last_count:
last_count = current_count
stagnant = 0
else:
stagnant += 1
if stagnant >= 10:
logger.info("评论数量停滞超过10次")
break
# 滚动到最后一条评论
if current_count > 0:
page.scroll_nth_element_into_view(PARENT_COMMENT, current_count - 1)
time.sleep(0.3)
# 继续滚动
page.evaluate("window.scrollBy(0, window.innerHeight * 0.8)")
time.sleep(0.5)
# 通过 commentID 查找
if comment_id:
selector = f"#comment-{comment_id}"
if page.has_element(selector):
logger.info("通过 commentID 找到评论 (尝试 %d 次)", attempt + 1)
page.scroll_element_into_view(selector)
return True
# 通过 userID 查找
if user_id:
found = page.evaluate(
f"""
(() => {{
const els = document.querySelectorAll(
'.parent-comment, .comment-item, .comment'
);
for (const el of els) {{
if (el.querySelector('[data-user-id="{user_id}"]')) {{
el.scrollIntoView({{behavior: 'smooth', block: 'center'}});
return true;
}}
}}
return false;
}})()
"""
)
if found:
logger.info("通过 userID 找到评论 (尝试 %d 次)", attempt + 1)
return True
time.sleep(0.8)
return False
def _js_str(s: str) -> str:
"""将 Python 字符串转为 JS 字面量(含引号)。"""
import json
return json.dumps(s)