xiaohongshu.py 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. # -*- coding: utf-8 -*-
  2. """
  3. 小红书视频发布器
  4. 参考: matrix/xhs_uploader/main.py
  5. 使用 xhs SDK API 方式发布,更稳定
  6. """
  7. import asyncio
  8. import os
  9. import sys
  10. import time
  11. from pathlib import Path
  12. from typing import List
  13. from .base import (
  14. BasePublisher, PublishParams, PublishResult,
  15. WorkItem, WorksResult, CommentItem, CommentsResult
  16. )
  17. from playwright.async_api import async_playwright
  18. # 添加 matrix 项目路径,用于导入签名脚本
  19. MATRIX_PATH = Path(__file__).parent.parent.parent.parent / "matrix"
  20. sys.path.insert(0, str(MATRIX_PATH))
  21. # 尝试导入 xhs SDK
  22. try:
  23. from xhs import XhsClient
  24. XHS_SDK_AVAILABLE = True
  25. except ImportError:
  26. print("[Warning] xhs 库未安装,请运行: pip install xhs")
  27. XhsClient = None
  28. XHS_SDK_AVAILABLE = False
  29. # 签名脚本路径
  30. STEALTH_JS_PATH = MATRIX_PATH / "xhs-api" / "js" / "stealth.min.js"
  31. class XiaohongshuPublisher(BasePublisher):
  32. """
  33. 小红书视频发布器
  34. 优先使用 xhs SDK API 方式发布
  35. """
  36. platform_name = "xiaohongshu"
  37. login_url = "https://creator.xiaohongshu.com/"
  38. publish_url = "https://creator.xiaohongshu.com/publish/publish"
  39. cookie_domain = ".xiaohongshu.com"
  40. async def get_sign(self, uri: str, data=None, a1: str = "", web_session: str = ""):
  41. """获取小红书 API 签名"""
  42. from playwright.async_api import async_playwright
  43. try:
  44. async with async_playwright() as playwright:
  45. browser = await playwright.chromium.launch(headless=True)
  46. browser_context = await browser.new_context()
  47. if STEALTH_JS_PATH.exists():
  48. await browser_context.add_init_script(path=str(STEALTH_JS_PATH))
  49. page = await browser_context.new_page()
  50. await page.goto("https://www.xiaohongshu.com")
  51. await asyncio.sleep(1)
  52. await page.reload()
  53. await asyncio.sleep(1)
  54. if a1:
  55. await browser_context.add_cookies([
  56. {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}
  57. ])
  58. await page.reload()
  59. await asyncio.sleep(0.5)
  60. encrypt_params = await page.evaluate(
  61. "([url, data]) => window._webmsxyw(url, data)",
  62. [uri, data]
  63. )
  64. await browser_context.close()
  65. await browser.close()
  66. return {
  67. "x-s": encrypt_params["X-s"],
  68. "x-t": str(encrypt_params["X-t"])
  69. }
  70. except Exception as e:
  71. import traceback
  72. traceback.print_exc()
  73. raise Exception(f"签名失败: {e}")
  74. def sign_sync(self, uri, data=None, a1="", web_session=""):
  75. """
  76. 同步签名函数,供 XhsClient 使用。
  77. 注意:发布流程运行在 asyncio 事件循环中(通过 asyncio.run 启动),
  78. 这里如果再调用 asyncio.run 会触发 “asyncio.run() cannot be called from a running event loop”。
  79. 因此改为使用 sync_playwright 的同步实现(参考 matrix/xhs_uploader)。
  80. """
  81. try:
  82. from playwright.sync_api import sync_playwright
  83. except Exception as e:
  84. raise Exception(f"缺少 playwright 同步接口支持: {e}")
  85. last_exc: Exception | None = None
  86. for attempt in range(1, 6):
  87. try:
  88. with sync_playwright() as playwright:
  89. browser = playwright.chromium.launch(headless=True)
  90. context = browser.new_context()
  91. if STEALTH_JS_PATH.exists():
  92. context.add_init_script(path=str(STEALTH_JS_PATH))
  93. page = context.new_page()
  94. page.goto("https://www.xiaohongshu.com", wait_until="domcontentloaded", timeout=60000)
  95. if a1:
  96. context.add_cookies([
  97. {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}
  98. ])
  99. page.reload(wait_until="domcontentloaded")
  100. # 参考 matrix:设置完 cookie 后需要稍等,否则可能出现 window._webmsxyw 不存在
  101. time.sleep(1.5)
  102. encrypt_params = page.evaluate(
  103. "([url, data]) => window._webmsxyw(url, data)",
  104. [uri, data]
  105. )
  106. context.close()
  107. browser.close()
  108. return {
  109. "x-s": encrypt_params["X-s"],
  110. "x-t": str(encrypt_params["X-t"])
  111. }
  112. except Exception as e:
  113. last_exc = e
  114. # 轻微退避重试
  115. time.sleep(0.4 * attempt)
  116. raise Exception(f"签名失败: {last_exc}")
  117. async def publish_via_api(self, cookies: str, params: PublishParams) -> PublishResult:
  118. """通过 API 发布视频"""
  119. if not XHS_SDK_AVAILABLE:
  120. raise Exception("xhs SDK 未安装,请运行: pip install xhs")
  121. self.report_progress(10, "正在通过 API 发布...")
  122. print(f"[{self.platform_name}] 使用 XHS SDK API 发布...")
  123. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  124. print(f"[{self.platform_name}] 标题: {params.title}")
  125. # 转换 cookie 格式
  126. cookie_list = self.parse_cookies(cookies)
  127. cookie_string = self.cookies_to_string(cookie_list) if cookie_list else cookies
  128. print(f"[{self.platform_name}] Cookie 长度: {len(cookie_string)}")
  129. self.report_progress(20, "正在上传视频...")
  130. # 创建客户端
  131. xhs_client = XhsClient(cookie_string, sign=self.sign_sync)
  132. print(f"[{self.platform_name}] 开始调用 create_video_note...")
  133. # 发布视频
  134. try:
  135. result = xhs_client.create_video_note(
  136. title=params.title,
  137. desc=params.description or params.title,
  138. topics=params.tags or [],
  139. post_time=params.publish_date.strftime("%Y-%m-%d %H:%M:%S") if params.publish_date else None,
  140. video_path=params.video_path,
  141. cover_path=params.cover_path if params.cover_path and os.path.exists(params.cover_path) else None
  142. )
  143. print(f"[{self.platform_name}] SDK 返回结果: {result}")
  144. except Exception as e:
  145. import traceback
  146. traceback.print_exc()
  147. print(f"[{self.platform_name}] SDK 调用失败: {e}")
  148. raise Exception(f"XHS SDK 发布失败: {e}")
  149. # 验证返回结果
  150. if not result:
  151. raise Exception("XHS SDK 返回空结果")
  152. # 检查是否有错误
  153. if isinstance(result, dict):
  154. if result.get("code") and result.get("code") != 0:
  155. raise Exception(f"发布失败: {result.get('msg', '未知错误')}")
  156. if result.get("success") == False:
  157. raise Exception(f"发布失败: {result.get('msg', result.get('error', '未知错误'))}")
  158. note_id = result.get("note_id", "") if isinstance(result, dict) else ""
  159. video_url = result.get("url", "") if isinstance(result, dict) else ""
  160. if not note_id:
  161. print(f"[{self.platform_name}] 警告: 未获取到 note_id,返回结果: {result}")
  162. self.report_progress(100, "发布成功")
  163. print(f"[{self.platform_name}] 发布成功! note_id={note_id}, url={video_url}")
  164. return PublishResult(
  165. success=True,
  166. platform=self.platform_name,
  167. video_id=note_id,
  168. video_url=video_url,
  169. message="发布成功"
  170. )
  171. async def publish(self, cookies: str, params: PublishParams) -> PublishResult:
  172. """发布视频到小红书 - 参考 matrix/xhs_uploader/main.py"""
  173. print(f"\n{'='*60}")
  174. print(f"[{self.platform_name}] 开始发布视频")
  175. print(f"[{self.platform_name}] 视频路径: {params.video_path}")
  176. print(f"[{self.platform_name}] 标题: {params.title}")
  177. print(f"[{self.platform_name}] Headless: {self.headless}")
  178. print(f"[{self.platform_name}] XHS SDK 可用: {XHS_SDK_AVAILABLE}")
  179. print(f"{'='*60}")
  180. # 检查视频文件
  181. if not os.path.exists(params.video_path):
  182. raise Exception(f"视频文件不存在: {params.video_path}")
  183. print(f"[{self.platform_name}] 视频文件存在,大小: {os.path.getsize(params.video_path)} bytes")
  184. self.report_progress(5, "正在准备发布...")
  185. # 参考 matrix: 优先使用 XHS SDK API 方式发布(更稳定)
  186. if XHS_SDK_AVAILABLE:
  187. try:
  188. print(f"[{self.platform_name}] 尝试使用 XHS SDK API 发布...")
  189. result = await self.publish_via_api(cookies, params)
  190. print(f"[{self.platform_name}] API 发布完成: success={result.success}")
  191. # 如果 API 返回成功,直接返回
  192. if result.success:
  193. return result
  194. # 如果 API 返回失败但有具体错误,也返回
  195. if result.error and "请刷新" not in result.error:
  196. return result
  197. # 其他情况尝试 Playwright 方式
  198. print(f"[{self.platform_name}] API 方式未成功,尝试 Playwright...")
  199. except Exception as e:
  200. import traceback
  201. traceback.print_exc()
  202. print(f"[{self.platform_name}] API 发布失败: {e}")
  203. print(f"[{self.platform_name}] 尝试使用 Playwright 方式...")
  204. # 使用 Playwright 方式发布
  205. print(f"[{self.platform_name}] 使用 Playwright 方式发布...")
  206. return await self.publish_via_playwright(cookies, params)
  207. async def publish_via_playwright(self, cookies: str, params: PublishParams) -> PublishResult:
  208. """通过 Playwright 发布视频"""
  209. self.report_progress(10, "正在初始化浏览器...")
  210. print(f"[{self.platform_name}] Playwright 方式开始...")
  211. await self.init_browser()
  212. cookie_list = self.parse_cookies(cookies)
  213. print(f"[{self.platform_name}] 设置 {len(cookie_list)} 个 cookies")
  214. await self.set_cookies(cookie_list)
  215. if not self.page:
  216. raise Exception("Page not initialized")
  217. self.report_progress(15, "正在打开发布页面...")
  218. # 直接访问视频发布页面
  219. publish_url = "https://creator.xiaohongshu.com/publish/publish?source=official"
  220. print(f"[{self.platform_name}] 打开页面: {publish_url}")
  221. await self.page.goto(publish_url)
  222. await asyncio.sleep(3)
  223. current_url = self.page.url
  224. print(f"[{self.platform_name}] 当前 URL: {current_url}")
  225. async def wait_for_manual_login(timeout_seconds: int = 300) -> bool:
  226. if not self.page:
  227. return False
  228. self.report_progress(12, "检测到需要登录,请在浏览器窗口完成登录...")
  229. try:
  230. await self.page.bring_to_front()
  231. except:
  232. pass
  233. waited = 0
  234. while waited < timeout_seconds:
  235. try:
  236. url = self.page.url
  237. if "login" not in url and "passport" not in url and "creator.xiaohongshu.com" in url:
  238. return True
  239. await asyncio.sleep(2)
  240. waited += 2
  241. except:
  242. await asyncio.sleep(2)
  243. waited += 2
  244. return False
  245. async def wait_for_manual_captcha(timeout_seconds: int = 180) -> bool:
  246. waited = 0
  247. while waited < timeout_seconds:
  248. try:
  249. ai_captcha = await self.ai_check_captcha()
  250. if not ai_captcha.get("has_captcha"):
  251. return True
  252. except:
  253. pass
  254. await asyncio.sleep(3)
  255. waited += 3
  256. return False
  257. # 检查登录状态
  258. if "login" in current_url or "passport" in current_url:
  259. if not self.headless:
  260. logged_in = await wait_for_manual_login()
  261. if logged_in:
  262. try:
  263. if self.context:
  264. cookies_after = await self.context.cookies()
  265. await self.sync_cookies_to_node(cookies_after)
  266. except:
  267. pass
  268. await self.page.goto(publish_url)
  269. await asyncio.sleep(3)
  270. current_url = self.page.url
  271. else:
  272. screenshot_base64 = await self.capture_screenshot()
  273. return PublishResult(
  274. success=False,
  275. platform=self.platform_name,
  276. error="需要登录:请在浏览器窗口完成登录后重试",
  277. screenshot_base64=screenshot_base64,
  278. page_url=current_url,
  279. status='need_captcha',
  280. need_captcha=True,
  281. captcha_type='login'
  282. )
  283. else:
  284. screenshot_base64 = await self.capture_screenshot()
  285. return PublishResult(
  286. success=False,
  287. platform=self.platform_name,
  288. error="登录已过期,请重新登录",
  289. screenshot_base64=screenshot_base64,
  290. page_url=current_url,
  291. status='need_captcha',
  292. need_captcha=True,
  293. captcha_type='login'
  294. )
  295. # 使用 AI 检查验证码
  296. ai_captcha = await self.ai_check_captcha()
  297. if ai_captcha['has_captcha']:
  298. print(f"[{self.platform_name}] AI检测到验证码: {ai_captcha['captcha_type']}", flush=True)
  299. if not self.headless:
  300. solved = await wait_for_manual_captcha()
  301. if solved:
  302. try:
  303. if self.context:
  304. cookies_after = await self.context.cookies()
  305. await self.sync_cookies_to_node(cookies_after)
  306. except:
  307. pass
  308. else:
  309. screenshot_base64 = await self.capture_screenshot()
  310. return PublishResult(
  311. success=False,
  312. platform=self.platform_name,
  313. error=f"需要验证码:请在浏览器窗口完成验证后重试",
  314. screenshot_base64=screenshot_base64,
  315. page_url=current_url,
  316. status='need_captcha',
  317. need_captcha=True,
  318. captcha_type=ai_captcha['captcha_type']
  319. )
  320. else:
  321. screenshot_base64 = await self.capture_screenshot()
  322. return PublishResult(
  323. success=False,
  324. platform=self.platform_name,
  325. error=f"检测到{ai_captcha['captcha_type']}验证码,需要使用有头浏览器完成验证",
  326. screenshot_base64=screenshot_base64,
  327. page_url=current_url,
  328. status='need_captcha',
  329. need_captcha=True,
  330. captcha_type=ai_captcha['captcha_type']
  331. )
  332. self.report_progress(20, "正在上传视频...")
  333. # 等待页面加载
  334. await asyncio.sleep(2)
  335. # 上传视频
  336. upload_triggered = False
  337. # 方法1: 直接设置隐藏的 file input
  338. print(f"[{self.platform_name}] 尝试方法1: 设置 file input")
  339. file_inputs = self.page.locator('input[type="file"]')
  340. input_count = await file_inputs.count()
  341. print(f"[{self.platform_name}] 找到 {input_count} 个 file input")
  342. if input_count > 0:
  343. # 找到接受视频的 input
  344. for i in range(input_count):
  345. input_el = file_inputs.nth(i)
  346. accept = await input_el.get_attribute('accept') or ''
  347. print(f"[{self.platform_name}] Input {i} accept: {accept}")
  348. if 'video' in accept or '*' in accept or not accept:
  349. await input_el.set_input_files(params.video_path)
  350. upload_triggered = True
  351. print(f"[{self.platform_name}] 视频文件已设置到 input {i}")
  352. break
  353. # 方法2: 点击上传区域触发文件选择器
  354. if not upload_triggered:
  355. print(f"[{self.platform_name}] 尝试方法2: 点击上传区域")
  356. try:
  357. upload_area = self.page.locator('[class*="upload-wrapper"], [class*="upload-area"], .upload-input').first
  358. if await upload_area.count() > 0:
  359. async with self.page.expect_file_chooser(timeout=5000) as fc_info:
  360. await upload_area.click()
  361. file_chooser = await fc_info.value
  362. await file_chooser.set_files(params.video_path)
  363. upload_triggered = True
  364. print(f"[{self.platform_name}] 通过点击上传区域上传成功")
  365. except Exception as e:
  366. print(f"[{self.platform_name}] 方法2失败: {e}")
  367. if not upload_triggered:
  368. screenshot_base64 = await self.capture_screenshot()
  369. page_url = await self.get_page_url()
  370. return PublishResult(
  371. success=False,
  372. platform=self.platform_name,
  373. error="无法上传视频文件",
  374. screenshot_base64=screenshot_base64,
  375. page_url=page_url,
  376. status='need_action'
  377. )
  378. self.report_progress(40, "等待视频上传完成...")
  379. print(f"[{self.platform_name}] 等待视频上传和处理...")
  380. # 等待上传完成(检测页面变化)
  381. upload_complete = False
  382. for i in range(60): # 最多等待3分钟
  383. await asyncio.sleep(3)
  384. # 检查是否有标题输入框(上传完成后出现)
  385. title_input_count = await self.page.locator('input[placeholder*="标题"], input[placeholder*="填写标题"]').count()
  386. # 或者检查编辑器区域
  387. editor_count = await self.page.locator('[class*="ql-editor"], [contenteditable="true"]').count()
  388. # 检查发布按钮是否可见
  389. publish_btn_count = await self.page.locator('.publishBtn, button:has-text("发布")').count()
  390. print(f"[{self.platform_name}] 检测 {i+1}: 标题框={title_input_count}, 编辑器={editor_count}, 发布按钮={publish_btn_count}")
  391. if title_input_count > 0 or (editor_count > 0 and publish_btn_count > 0):
  392. upload_complete = True
  393. print(f"[{self.platform_name}] 视频上传完成!")
  394. break
  395. if not upload_complete:
  396. screenshot_base64 = await self.capture_screenshot()
  397. page_url = await self.get_page_url()
  398. return PublishResult(
  399. success=False,
  400. platform=self.platform_name,
  401. error="视频上传超时",
  402. screenshot_base64=screenshot_base64,
  403. page_url=page_url,
  404. status='need_action'
  405. )
  406. await asyncio.sleep(2)
  407. self.report_progress(60, "正在填写笔记信息...")
  408. print(f"[{self.platform_name}] 填写标题: {params.title[:20]}")
  409. # 填写标题
  410. title_filled = False
  411. title_selectors = [
  412. 'input[placeholder*="标题"]',
  413. 'input[placeholder*="填写标题"]',
  414. '[class*="title"] input',
  415. '.c-input_inner',
  416. ]
  417. for selector in title_selectors:
  418. title_input = self.page.locator(selector).first
  419. if await title_input.count() > 0:
  420. await title_input.click()
  421. await title_input.fill('') # 先清空
  422. await title_input.fill(params.title[:20])
  423. title_filled = True
  424. print(f"[{self.platform_name}] 标题已填写,使用选择器: {selector}")
  425. break
  426. if not title_filled:
  427. print(f"[{self.platform_name}] 警告: 未找到标题输入框")
  428. # 填写描述和标签
  429. if params.description or params.tags:
  430. desc_filled = False
  431. desc_selectors = [
  432. '[class*="ql-editor"]',
  433. '[class*="content-input"] [contenteditable="true"]',
  434. '[class*="editor"] [contenteditable="true"]',
  435. '.ql-editor',
  436. ]
  437. for selector in desc_selectors:
  438. desc_input = self.page.locator(selector).first
  439. if await desc_input.count() > 0:
  440. await desc_input.click()
  441. await asyncio.sleep(0.5)
  442. if params.description:
  443. await self.page.keyboard.type(params.description, delay=20)
  444. print(f"[{self.platform_name}] 描述已填写")
  445. if params.tags:
  446. # 添加标签
  447. await self.page.keyboard.press("Enter")
  448. for tag in params.tags[:5]: # 最多5个标签
  449. await self.page.keyboard.type(f"#{tag}", delay=20)
  450. await asyncio.sleep(0.3)
  451. await self.page.keyboard.press("Space")
  452. print(f"[{self.platform_name}] 标签已填写: {params.tags[:5]}")
  453. desc_filled = True
  454. break
  455. if not desc_filled:
  456. print(f"[{self.platform_name}] 警告: 未找到描述输入框")
  457. await asyncio.sleep(2)
  458. self.report_progress(80, "正在发布...")
  459. await asyncio.sleep(2)
  460. # 滚动到页面底部确保发布按钮可见
  461. await self.page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  462. await asyncio.sleep(1)
  463. print(f"[{self.platform_name}] 查找发布按钮...")
  464. # 点击发布
  465. publish_selectors = [
  466. 'button.publishBtn',
  467. '.publishBtn',
  468. 'button.d-button.red',
  469. 'button:has-text("发布"):not(:has-text("定时发布"))',
  470. '[class*="publish"][class*="btn"]',
  471. ]
  472. publish_clicked = False
  473. for selector in publish_selectors:
  474. try:
  475. btn = self.page.locator(selector).first
  476. if await btn.count() > 0:
  477. is_visible = await btn.is_visible()
  478. is_enabled = await btn.is_enabled()
  479. print(f"[{self.platform_name}] 按钮 {selector}: visible={is_visible}, enabled={is_enabled}")
  480. if is_visible and is_enabled:
  481. box = await btn.bounding_box()
  482. if box:
  483. print(f"[{self.platform_name}] 点击发布按钮: {selector}, 位置: ({box['x']}, {box['y']})")
  484. # 使用真实鼠标点击
  485. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  486. publish_clicked = True
  487. break
  488. except Exception as e:
  489. print(f"[{self.platform_name}] 选择器 {selector} 错误: {e}")
  490. if not publish_clicked:
  491. try:
  492. suggest = await self.ai_suggest_playwright_selector("点击小红书发布按钮")
  493. if suggest.get("has_selector") and suggest.get("selector"):
  494. sel = suggest.get("selector")
  495. btn = self.page.locator(sel).first
  496. if await btn.count() > 0 and await btn.is_visible() and await btn.is_enabled():
  497. try:
  498. await btn.click()
  499. except:
  500. box = await btn.bounding_box()
  501. if box:
  502. await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
  503. publish_clicked = True
  504. except Exception as e:
  505. print(f"[{self.platform_name}] AI 点击发布按钮失败: {e}", flush=True)
  506. if not publish_clicked:
  507. # 保存截图用于调试
  508. screenshot_path = f"debug_publish_failed_{self.platform_name}.png"
  509. await self.page.screenshot(path=screenshot_path, full_page=True)
  510. print(f"[{self.platform_name}] 未找到发布按钮,截图保存到: {screenshot_path}")
  511. # 打印页面 HTML 结构用于调试
  512. buttons = await self.page.query_selector_all('button')
  513. print(f"[{self.platform_name}] 页面上共有 {len(buttons)} 个按钮")
  514. for i, btn in enumerate(buttons[:10]):
  515. text = await btn.text_content() or ''
  516. cls = await btn.get_attribute('class') or ''
  517. print(f" 按钮 {i}: text='{text.strip()[:30]}', class='{cls[:50]}'")
  518. raise Exception("未找到发布按钮")
  519. print(f"[{self.platform_name}] 已点击发布按钮,等待发布完成...")
  520. self.report_progress(90, "等待发布结果...")
  521. # 等待发布完成(检测 URL 变化或成功提示)
  522. publish_success = False
  523. for i in range(20): # 最多等待 20 秒
  524. await asyncio.sleep(1)
  525. current_url = self.page.url
  526. # 检查是否跳转到发布成功页面或内容管理页面
  527. if "published=true" in current_url or "success" in current_url or "content" in current_url:
  528. publish_success = True
  529. print(f"[{self.platform_name}] 发布成功! 跳转到: {current_url}")
  530. break
  531. # 检查是否有成功提示
  532. try:
  533. success_msg = await self.page.locator('[class*="success"], .toast-success, [class*="Toast"]').first.is_visible()
  534. if success_msg:
  535. publish_success = True
  536. print(f"[{self.platform_name}] 检测到成功提示!")
  537. break
  538. except:
  539. pass
  540. # 检查是否有错误提示
  541. try:
  542. error_elements = self.page.locator('[class*="error"], .toast-error, [class*="fail"]')
  543. if await error_elements.count() > 0:
  544. error_text = await error_elements.first.text_content()
  545. if error_text and len(error_text.strip()) > 0:
  546. raise Exception(f"发布失败: {error_text.strip()}")
  547. except Exception as e:
  548. if "发布失败" in str(e):
  549. raise
  550. # 如果没有明确的成功标志,返回截图供 AI 分析
  551. if not publish_success:
  552. final_url = self.page.url
  553. print(f"[{self.platform_name}] 发布结果不确定,当前 URL: {final_url}")
  554. screenshot_base64 = await self.capture_screenshot()
  555. print(f"[{self.platform_name}] 已获取截图供 AI 分析")
  556. # 如果 URL 还是发布页面,可能需要继续操作
  557. if "publish/publish" in final_url:
  558. return PublishResult(
  559. success=False,
  560. platform=self.platform_name,
  561. error="发布结果待确认,请查看截图",
  562. screenshot_base64=screenshot_base64,
  563. page_url=final_url,
  564. status='need_action'
  565. )
  566. self.report_progress(100, "发布完成")
  567. print(f"[{self.platform_name}] Playwright 方式发布完成!")
  568. screenshot_base64 = await self.capture_screenshot()
  569. page_url = await self.get_page_url()
  570. return PublishResult(
  571. success=True,
  572. platform=self.platform_name,
  573. message="发布完成",
  574. screenshot_base64=screenshot_base64,
  575. page_url=page_url,
  576. status='success'
  577. )
  578. async def get_account_info(self, cookies: str) -> dict:
  579. """获取账号信息"""
  580. print(f"\n{'='*60}")
  581. print(f"[{self.platform_name}] 获取账号信息")
  582. print(f"{'='*60}")
  583. captured_info = {}
  584. try:
  585. await self.init_browser()
  586. cookie_list = self.parse_cookies(cookies)
  587. await self.set_cookies(cookie_list)
  588. if not self.page:
  589. raise Exception("Page not initialized")
  590. # 监听个人信息 API
  591. async def handle_response(response):
  592. nonlocal captured_info
  593. if 'api/galaxy/creator/home/personal_info' in response.url:
  594. try:
  595. json_data = await response.json()
  596. print(f"[{self.platform_name}] 捕获个人信息 API", flush=True)
  597. if json_data.get('success') or json_data.get('code') == 0:
  598. data = json_data.get('data', {})
  599. captured_info = {
  600. "account_id": f"xhs_{data.get('red_num', '')}",
  601. "account_name": data.get('name', ''),
  602. "avatar_url": data.get('avatar', ''),
  603. "fans_count": data.get('fans_count', 0),
  604. "works_count": 0 # 暂时无法直接获取准确的作品数,需要从作品列表获取
  605. }
  606. except Exception as e:
  607. print(f"[{self.platform_name}] 解析个人信息失败: {e}", flush=True)
  608. self.page.on('response', handle_response)
  609. # 访问首页
  610. print(f"[{self.platform_name}] 访问创作者首页...", flush=True)
  611. await self.page.goto("https://creator.xiaohongshu.com/new/home", wait_until="domcontentloaded")
  612. # 等待 API 响应
  613. for _ in range(10):
  614. if captured_info:
  615. break
  616. await asyncio.sleep(1)
  617. if not captured_info:
  618. print(f"[{self.platform_name}] 未捕获到个人信息,尝试刷新...", flush=True)
  619. await self.page.reload()
  620. for _ in range(10):
  621. if captured_info:
  622. break
  623. await asyncio.sleep(1)
  624. if not captured_info:
  625. raise Exception("无法获取账号信息")
  626. # 尝试获取作品数(从首页或其他地方)
  627. # 或者简单地返回已获取的信息,作品数由 get_works 更新
  628. return {
  629. "success": True,
  630. **captured_info
  631. }
  632. except Exception as e:
  633. import traceback
  634. traceback.print_exc()
  635. return {
  636. "success": False,
  637. "error": str(e)
  638. }
  639. finally:
  640. await self.close_browser()
  641. async def get_works(self, cookies: str, page: int = 0, page_size: int = 20) -> WorksResult:
  642. """获取小红书作品列表 - 通过直接调用创作者笔记列表 API 获取"""
  643. print(f"\n{'='*60}", flush=True)
  644. print(f"[{self.platform_name}] 获取作品列表", flush=True)
  645. print(f"[{self.platform_name}] page={page}, page_size={page_size}", flush=True)
  646. print(f"{'='*60}", flush=True)
  647. works: List[WorkItem] = []
  648. total = 0
  649. has_more = False
  650. next_page = ""
  651. api_page_size = 20
  652. try:
  653. await self.init_browser()
  654. cookie_list = self.parse_cookies(cookies)
  655. # 打印 cookies 信息用于调试
  656. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  657. await self.set_cookies(cookie_list)
  658. if not self.page:
  659. raise Exception("Page not initialized")
  660. # 访问笔记管理页面 - 页面会自动发起 API 请求
  661. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  662. try:
  663. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=30000)
  664. except Exception as nav_error:
  665. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  666. # 检查登录状态
  667. current_url = self.page.url
  668. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  669. if "login" in current_url:
  670. raise Exception("Cookie 已过期,请重新登录")
  671. # 等待页面完全加载,确保签名函数可用
  672. print(f"[{self.platform_name}] 等待页面完全加载和签名函数初始化...", flush=True)
  673. await asyncio.sleep(3)
  674. # 检查签名函数是否可用
  675. sign_check_attempts = 0
  676. max_sign_check_attempts = 10
  677. while sign_check_attempts < max_sign_check_attempts:
  678. sign_available = await self.page.evaluate("""() => {
  679. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  680. }""")
  681. if sign_available:
  682. print(f"[{self.platform_name}] ✓ 签名函数 _webmsxyw 已可用", flush=True)
  683. break
  684. sign_check_attempts += 1
  685. print(f"[{self.platform_name}] ⏳ 等待签名函数... ({sign_check_attempts}/{max_sign_check_attempts})", flush=True)
  686. await asyncio.sleep(1)
  687. if sign_check_attempts >= max_sign_check_attempts:
  688. print(f"[{self.platform_name}] ⚠️ 警告: 签名函数 _webmsxyw 在 {max_sign_check_attempts} 次检查后仍不可用", flush=True)
  689. print(f"[{self.platform_name}] 继续尝试,但 API 调用可能会失败", flush=True)
  690. async def fetch_notes_page(p):
  691. # 再次检查签名函数(每次调用前都检查)
  692. sign_available = await self.page.evaluate("""() => {
  693. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  694. }""")
  695. if not sign_available:
  696. print(f"[{self.platform_name}] ⚠️ 签名函数 _webmsxyw 不可用,等待...", flush=True)
  697. await asyncio.sleep(2)
  698. return await self.page.evaluate(
  699. """async (pageNum) => {
  700. try {
  701. // 使用正确的 API 端点:/api/galaxy/v2/creator/note/user/posted
  702. const url = `/api/galaxy/v2/creator/note/user/posted?tab=0&page=${pageNum}`;
  703. const headers = {
  704. 'Accept': 'application/json, text/plain, */*',
  705. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  706. 'Referer': 'https://creator.xiaohongshu.com/new/note-manager',
  707. 'Sec-Fetch-Dest': 'empty',
  708. 'Sec-Fetch-Mode': 'cors',
  709. 'Sec-Fetch-Site': 'same-origin'
  710. };
  711. // 尝试获取签名
  712. let signResult = { hasSign: false, x_s: '', x_t: '', x_s_common: '', error: '' };
  713. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  714. try {
  715. const sign = window._webmsxyw(url, '');
  716. headers['x-s'] = sign['X-s'];
  717. headers['x-t'] = String(sign['X-t']);
  718. // 检查是否有 x-s-common
  719. if (sign['X-s-common']) {
  720. headers['x-s-common'] = sign['X-s-common'];
  721. }
  722. signResult = {
  723. hasSign: true,
  724. x_s: sign['X-s'] ? sign['X-s'].substring(0, 50) + '...' : '',
  725. x_t: String(sign['X-t']),
  726. x_s_common: sign['X-s-common'] ? sign['X-s-common'].substring(0, 50) + '...' : '',
  727. error: ''
  728. };
  729. console.log('签名生成成功:', signResult);
  730. } catch (e) {
  731. signResult.error = e.toString();
  732. console.error('签名生成失败:', e);
  733. }
  734. } else {
  735. signResult.error = '_webmsxyw function not found';
  736. console.error('签名函数不存在');
  737. }
  738. const res = await fetch(url, {
  739. method: 'GET',
  740. credentials: 'include',
  741. headers
  742. });
  743. const responseData = await res.json();
  744. return {
  745. ...responseData,
  746. _debug: {
  747. signResult: signResult,
  748. status: res.status,
  749. statusText: res.statusText
  750. }
  751. };
  752. } catch (e) {
  753. return { success: false, error: e.toString() };
  754. }
  755. }""",
  756. p
  757. )
  758. def parse_notes(notes_list):
  759. parsed = []
  760. for note in notes_list:
  761. note_id = note.get('id', '')
  762. if not note_id:
  763. continue
  764. cover_url = ''
  765. images_list = note.get('images_list', [])
  766. if images_list:
  767. cover_url = images_list[0].get('url', '')
  768. if cover_url.startswith('http://'):
  769. cover_url = cover_url.replace('http://', 'https://')
  770. duration = note.get('video_info', {}).get('duration', 0)
  771. status = 'published'
  772. tab_status = note.get('tab_status', 1)
  773. if tab_status == 0:
  774. status = 'draft'
  775. elif tab_status == 2:
  776. status = 'reviewing'
  777. elif tab_status == 3:
  778. status = 'rejected'
  779. video_url = f"https://www.xiaohongshu.com/explore/{note_id}" if note_id else ""
  780. parsed.append(WorkItem(
  781. work_id=note_id,
  782. title=note.get('display_title', '') or '无标题',
  783. cover_url=cover_url,
  784. video_url=video_url,
  785. duration=duration,
  786. status=status,
  787. publish_time=note.get('time', ''),
  788. play_count=note.get('view_count', 0),
  789. like_count=note.get('likes', 0),
  790. comment_count=note.get('comments_count', 0),
  791. share_count=note.get('shared_count', 0),
  792. collect_count=note.get('collected_count', 0),
  793. ))
  794. return parsed
  795. resp = None
  796. for attempt in range(1, 4):
  797. resp = await fetch_notes_page(page)
  798. # 打印调试信息
  799. if resp and isinstance(resp, dict) and resp.get('_debug'):
  800. debug_info = resp.get('_debug', {})
  801. sign_result = debug_info.get('signResult', {})
  802. print(f"[{self.platform_name}] 🔍 调试信息: 签名可用: {sign_result.get('hasSign', False)}, X-S: {sign_result.get('x_s', '')}, X-T: {sign_result.get('x_t', '')}, X-S-Common: {sign_result.get('x_s_common', '')}, 签名错误: {sign_result.get('error', '')}, HTTP 状态: {debug_info.get('status', 'N/A')}", flush=True)
  803. resp.pop('_debug', None)
  804. if resp and (resp.get('success') or resp.get('code') == 0) and resp.get('data'):
  805. break
  806. print(f"[{self.platform_name}] 拉取作品列表失败,重试 {attempt}/3: {str(resp)[:200]}", flush=True)
  807. await asyncio.sleep(1.2 * attempt)
  808. if not resp or not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  809. error_msg = resp.get('msg') if isinstance(resp, dict) else str(resp)
  810. # 打印详细的错误信息
  811. if isinstance(resp, dict):
  812. if resp.get('msg'):
  813. print(f"[{self.platform_name}] 错误消息: {resp.get('msg')}", flush=True)
  814. if resp.get('message'):
  815. print(f"[{self.platform_name}] 错误消息: {resp.get('message')}", flush=True)
  816. if resp.get('error'):
  817. print(f"[{self.platform_name}] 错误: {resp.get('error')}", flush=True)
  818. raise Exception(f"无法获取作品列表数据: {error_msg}")
  819. data = resp.get('data', {}) or {}
  820. notes = data.get('notes', []) or []
  821. print(f"[{self.platform_name}] 第 {page} 页 notes 数量: {len(notes)}", flush=True)
  822. tags = data.get('tags', []) or []
  823. if tags:
  824. preferred = 0
  825. for tag in tags:
  826. if tag.get('id') == 'special.note_time_desc':
  827. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  828. break
  829. if preferred:
  830. total = preferred
  831. else:
  832. total = max([int(t.get('notes_count', 0) or t.get('notesCount', 0) or t.get('count', 0) or 0) for t in tags] + [0])
  833. if not total:
  834. total = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  835. if not total and isinstance(data.get('page', {}), dict):
  836. total = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  837. next_page = data.get('page', "")
  838. if next_page == page:
  839. next_page = page + 1
  840. works.extend(parse_notes(notes))
  841. if total:
  842. has_more = (page * api_page_size + len(notes)) < total
  843. if has_more and (next_page == -1 or str(next_page) == "-1" or next_page == "" or next_page is None):
  844. next_page = page + 1
  845. else:
  846. if len(notes) == 0:
  847. has_more = False
  848. else:
  849. next_resp = await fetch_notes_page(page + 1)
  850. next_data = (next_resp or {}).get('data', {}) if isinstance(next_resp, dict) else {}
  851. next_notes = next_data.get('notes', []) or []
  852. has_more = len(next_notes) > 0
  853. next_page = next_data.get('page', next_page)
  854. except Exception as e:
  855. import traceback
  856. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  857. traceback.print_exc()
  858. return WorksResult(
  859. success=False,
  860. platform=self.platform_name,
  861. error=str(e)
  862. )
  863. finally:
  864. # 确保关闭浏览器
  865. await self.close_browser()
  866. return WorksResult(
  867. success=True,
  868. platform=self.platform_name,
  869. works=works,
  870. total=total or (page * api_page_size + len(works)),
  871. has_more=has_more,
  872. next_page=next_page
  873. )
  874. async def get_all_works(self, cookies: str) -> WorksResult:
  875. """获取小红书全部作品(单次请求内自动翻页抓全量,避免 Node 侧分页不一致)"""
  876. print(f"\n{'='*60}", flush=True)
  877. print(f"[{self.platform_name}] 获取全部作品(auto paging)", flush=True)
  878. print(f"{'='*60}", flush=True)
  879. works: List[WorkItem] = []
  880. total = 0
  881. seen_ids = set()
  882. cursor: object = 0
  883. max_iters = 800
  884. api_page_size = 20
  885. try:
  886. await self.init_browser()
  887. cookie_list = self.parse_cookies(cookies)
  888. print(f"[{self.platform_name}] 解析到 {len(cookie_list)} 个 cookies", flush=True)
  889. await self.set_cookies(cookie_list)
  890. if not self.page:
  891. raise Exception("Page not initialized")
  892. print(f"[{self.platform_name}] 访问笔记管理页面...", flush=True)
  893. try:
  894. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=60000)
  895. print(f"[{self.platform_name}] 页面加载成功", flush=True)
  896. except Exception as nav_error:
  897. print(f"[{self.platform_name}] 导航超时,但继续尝试: {nav_error}", flush=True)
  898. # 即使超时也检查当前页面状态
  899. try:
  900. await asyncio.sleep(2)
  901. current_url = self.page.url
  902. print(f"[{self.platform_name}] 超时后当前页面: {current_url}", flush=True)
  903. except Exception as e:
  904. print(f"[{self.platform_name}] 检查页面状态时出错: {e}", flush=True)
  905. current_url = self.page.url
  906. print(f"[{self.platform_name}] 当前页面: {current_url}", flush=True)
  907. if "login" in current_url:
  908. raise Exception("Cookie 已过期,请重新登录")
  909. # 等待页面完全加载,确保签名函数可用
  910. print(f"[{self.platform_name}] 等待页面完全加载和签名函数初始化...", flush=True)
  911. await asyncio.sleep(3)
  912. # 检查签名函数是否可用
  913. sign_check_attempts = 0
  914. max_sign_check_attempts = 10
  915. while sign_check_attempts < max_sign_check_attempts:
  916. sign_available = await self.page.evaluate("""() => {
  917. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  918. }""")
  919. if sign_available:
  920. print(f"[{self.platform_name}] ✓ 签名函数 _webmsxyw 已可用", flush=True)
  921. break
  922. sign_check_attempts += 1
  923. print(f"[{self.platform_name}] ⏳ 等待签名函数... ({sign_check_attempts}/{max_sign_check_attempts})", flush=True)
  924. await asyncio.sleep(1)
  925. if sign_check_attempts >= max_sign_check_attempts:
  926. print(f"[{self.platform_name}] ⚠️ 警告: 签名函数 _webmsxyw 在 {max_sign_check_attempts} 次检查后仍不可用", flush=True)
  927. print(f"[{self.platform_name}] 继续尝试,但 API 调用可能会失败", flush=True)
  928. async def fetch_notes_page(p):
  929. # 再次检查签名函数(每次调用前都检查)
  930. sign_available = await self.page.evaluate("""() => {
  931. return typeof window !== 'undefined' && typeof window._webmsxyw === 'function';
  932. }""")
  933. if not sign_available:
  934. print(f"[{self.platform_name}] ⚠️ 签名函数 _webmsxyw 不可用,等待...", flush=True)
  935. await asyncio.sleep(2)
  936. return await self.page.evaluate(
  937. """async (pageNum) => {
  938. try {
  939. // 使用正确的 API 端点:/api/galaxy/v2/creator/note/user/posted
  940. const url = `/api/galaxy/v2/creator/note/user/posted?tab=0&page=${pageNum}`;
  941. const headers = {
  942. 'Accept': 'application/json, text/plain, */*',
  943. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  944. 'Referer': 'https://creator.xiaohongshu.com/new/note-manager',
  945. 'Sec-Fetch-Dest': 'empty',
  946. 'Sec-Fetch-Mode': 'cors',
  947. 'Sec-Fetch-Site': 'same-origin'
  948. };
  949. // 尝试获取签名
  950. let signResult = { hasSign: false, x_s: '', x_t: '', x_s_common: '', error: '' };
  951. if (typeof window !== 'undefined' && typeof window._webmsxyw === 'function') {
  952. try {
  953. const sign = window._webmsxyw(url, '');
  954. headers['x-s'] = sign['X-s'];
  955. headers['x-t'] = String(sign['X-t']);
  956. // 检查是否有 x-s-common
  957. if (sign['X-s-common']) {
  958. headers['x-s-common'] = sign['X-s-common'];
  959. }
  960. signResult = {
  961. hasSign: true,
  962. x_s: sign['X-s'] ? sign['X-s'].substring(0, 50) + '...' : '',
  963. x_t: String(sign['X-t']),
  964. x_s_common: sign['X-s-common'] ? sign['X-s-common'].substring(0, 50) + '...' : '',
  965. error: ''
  966. };
  967. console.log('签名生成成功:', signResult);
  968. } catch (e) {
  969. signResult.error = e.toString();
  970. console.error('签名生成失败:', e);
  971. }
  972. } else {
  973. signResult.error = '_webmsxyw function not found';
  974. console.error('签名函数不存在');
  975. }
  976. const res = await fetch(url, {
  977. method: 'GET',
  978. credentials: 'include',
  979. headers
  980. });
  981. const responseData = await res.json();
  982. return {
  983. ...responseData,
  984. _debug: {
  985. signResult: signResult,
  986. status: res.status,
  987. statusText: res.statusText
  988. }
  989. };
  990. } catch (e) {
  991. return { success: false, error: e.toString() };
  992. }
  993. }""",
  994. p
  995. )
  996. def parse_notes(notes_list):
  997. parsed = []
  998. for note in notes_list:
  999. note_id = note.get('id', '')
  1000. if not note_id:
  1001. continue
  1002. cover_url = ''
  1003. images_list = note.get('images_list', [])
  1004. if images_list:
  1005. cover_url = images_list[0].get('url', '')
  1006. if cover_url.startswith('http://'):
  1007. cover_url = cover_url.replace('http://', 'https://')
  1008. duration = note.get('video_info', {}).get('duration', 0)
  1009. status = 'published'
  1010. tab_status = note.get('tab_status', 1)
  1011. if tab_status == 0:
  1012. status = 'draft'
  1013. elif tab_status == 2:
  1014. status = 'reviewing'
  1015. elif tab_status == 3:
  1016. status = 'rejected'
  1017. video_url = f"https://www.xiaohongshu.com/explore/{note_id}" if note_id else ""
  1018. parsed.append(WorkItem(
  1019. work_id=note_id,
  1020. title=note.get('display_title', '') or '无标题',
  1021. cover_url=cover_url,
  1022. video_url=video_url,
  1023. duration=duration,
  1024. status=status,
  1025. publish_time=note.get('time', ''),
  1026. play_count=note.get('view_count', 0),
  1027. like_count=note.get('likes', 0),
  1028. comment_count=note.get('comments_count', 0),
  1029. share_count=note.get('shared_count', 0),
  1030. collect_count=note.get('collected_count', 0),
  1031. ))
  1032. return parsed
  1033. async def collect_by_scrolling() -> WorksResult:
  1034. print(f"[{self.platform_name}] 直连接口被拒绝,切换为滚动页面 + 监听 API 响应模式", flush=True)
  1035. captured: List[WorkItem] = []
  1036. captured_total = 0
  1037. captured_seen = set()
  1038. lock = asyncio.Lock()
  1039. async def handle_response(response):
  1040. nonlocal captured_total
  1041. url = response.url
  1042. if ("creator.xiaohongshu.com" not in url and "edith.xiaohongshu.com" not in url) or "creator/note/user/posted" not in url:
  1043. return
  1044. try:
  1045. json_data = await response.json()
  1046. except Exception:
  1047. return
  1048. if not isinstance(json_data, dict):
  1049. return
  1050. if not (json_data.get("success") or json_data.get("code") == 0) or not json_data.get("data"):
  1051. return
  1052. data = json_data.get("data", {}) or {}
  1053. notes = data.get("notes", []) or []
  1054. tags = data.get("tags", []) or []
  1055. declared = 0
  1056. if tags:
  1057. preferred = 0
  1058. for tag in tags:
  1059. if tag.get("id") == "special.note_time_desc":
  1060. preferred = tag.get("notes_count", 0) or tag.get("notesCount", 0) or tag.get("count", 0) or 0
  1061. break
  1062. if preferred:
  1063. declared = int(preferred)
  1064. else:
  1065. declared = max([int(t.get("notes_count", 0) or t.get("notesCount", 0) or t.get("count", 0) or 0) for t in tags] + [0])
  1066. if not declared:
  1067. declared = int(data.get("total", 0) or data.get("total_count", 0) or data.get("totalCount", 0) or 0)
  1068. if not declared and isinstance(data.get("page", {}), dict):
  1069. declared = int(data.get("page", {}).get("total", 0) or data.get("page", {}).get("totalCount", 0) or 0)
  1070. async with lock:
  1071. if declared:
  1072. captured_total = max(captured_total, declared)
  1073. parsed = parse_notes(notes)
  1074. new_count = 0
  1075. for w in parsed:
  1076. if w.work_id and w.work_id not in captured_seen:
  1077. captured_seen.add(w.work_id)
  1078. captured.append(w)
  1079. new_count += 1
  1080. if new_count > 0:
  1081. print(
  1082. f"[{self.platform_name}] 捕获 notes 响应: notes={len(notes)}, new={new_count}, total_now={len(captured)}, declared_total={captured_total}",
  1083. flush=True
  1084. )
  1085. self.page.on("response", handle_response)
  1086. try:
  1087. try:
  1088. # 使用更宽松的等待条件,避免超时
  1089. await self.page.goto("https://creator.xiaohongshu.com/new/note-manager", wait_until="domcontentloaded", timeout=90000)
  1090. print(f"[{self.platform_name}] 页面加载成功", flush=True)
  1091. except Exception as nav_error:
  1092. print(f"[{self.platform_name}] 导航异常(继续):{nav_error}", flush=True)
  1093. # 即使超时也继续尝试,可能页面已经部分加载
  1094. try:
  1095. await asyncio.sleep(3)
  1096. current_url = self.page.url
  1097. print(f"[{self.platform_name}] 超时后当前页面: {current_url}", flush=True)
  1098. if "login" in current_url:
  1099. raise Exception("Cookie 已过期,请重新登录")
  1100. except Exception as e:
  1101. if "Cookie" in str(e):
  1102. raise
  1103. print(f"[{self.platform_name}] 检查页面状态时出错: {e}", flush=True)
  1104. await asyncio.sleep(2.0)
  1105. idle_rounds = 0
  1106. last_count = 0
  1107. last_height = 0
  1108. for _ in range(1, 400):
  1109. scroll_state = await self.page.evaluate(
  1110. """() => {
  1111. const isScrollable = (el) => {
  1112. if (!el) return false;
  1113. const style = window.getComputedStyle(el);
  1114. const oy = style.overflowY;
  1115. return (oy === 'auto' || oy === 'scroll') && (el.scrollHeight - el.clientHeight > 200);
  1116. };
  1117. const pickBest = () => {
  1118. const nodes = Array.from(document.querySelectorAll('*'));
  1119. let best = document.scrollingElement || document.documentElement || document.body;
  1120. let bestScroll = (best.scrollHeight || 0) - (best.clientHeight || 0);
  1121. for (const el of nodes) {
  1122. if (!isScrollable(el)) continue;
  1123. const diff = el.scrollHeight - el.clientHeight;
  1124. if (diff > bestScroll) {
  1125. best = el;
  1126. bestScroll = diff;
  1127. }
  1128. }
  1129. return best;
  1130. };
  1131. const el = pickBest();
  1132. const beforeTop = el.scrollTop || 0;
  1133. const beforeHeight = el.scrollHeight || 0;
  1134. el.scrollTo(0, beforeHeight);
  1135. return {
  1136. beforeTop,
  1137. afterTop: el.scrollTop || 0,
  1138. height: el.scrollHeight || 0,
  1139. client: el.clientHeight || 0,
  1140. };
  1141. }"""
  1142. )
  1143. await asyncio.sleep(1.2)
  1144. async with lock:
  1145. count_now = len(captured)
  1146. total_now = captured_total
  1147. if total_now and count_now >= total_now:
  1148. break
  1149. height_now = int(scroll_state.get("height", 0) or 0) if isinstance(scroll_state, dict) else 0
  1150. if count_now == last_count and height_now == last_height:
  1151. idle_rounds += 1
  1152. else:
  1153. idle_rounds = 0
  1154. last_count = count_now
  1155. last_height = height_now
  1156. if idle_rounds >= 6:
  1157. break
  1158. async with lock:
  1159. final_works = list(captured)
  1160. final_total = captured_total or len(final_works)
  1161. return WorksResult(
  1162. success=True,
  1163. platform=self.platform_name,
  1164. works=final_works,
  1165. total=final_total,
  1166. has_more=False,
  1167. next_page=-1
  1168. )
  1169. finally:
  1170. try:
  1171. self.page.remove_listener("response", handle_response)
  1172. except Exception:
  1173. pass
  1174. # 添加请求监听,捕获请求头信息
  1175. captured_requests = []
  1176. async def handle_request(request):
  1177. url = request.url
  1178. if ("creator.xiaohongshu.com" in url or "edith.xiaohongshu.com" in url) and "creator/note/user/posted" in url:
  1179. headers = request.headers
  1180. captured_requests.append({
  1181. "url": url,
  1182. "method": request.method,
  1183. "headers": dict(headers),
  1184. "timestamp": asyncio.get_event_loop().time()
  1185. })
  1186. # 打印关键头部信息
  1187. x_s = headers.get('x-s', '')
  1188. x_t = headers.get('x-t', '')
  1189. x_s_common = headers.get('x-s-common', '')
  1190. print(f"[{self.platform_name}] 📡 API 请求: {url}", flush=True)
  1191. print(f"[{self.platform_name}] Method: {request.method}", flush=True)
  1192. print(f"[{self.platform_name}] X-S: {x_s[:50] if x_s else '(none)'}...", flush=True)
  1193. print(f"[{self.platform_name}] X-T: {x_t}", flush=True)
  1194. print(f"[{self.platform_name}] X-S-Common: {x_s_common[:50] if x_s_common else '(none)'}...", flush=True)
  1195. print(f"[{self.platform_name}] Cookie: {headers.get('cookie', '')[:100]}...", flush=True)
  1196. self.page.on("request", handle_request)
  1197. iters = 0
  1198. page_count = 0 # 统计实际获取到的页数
  1199. print(f"[{self.platform_name}] ========== 开始自动分页获取作品 ==========", flush=True)
  1200. print(f"[{self.platform_name}] 最大迭代次数: {max_iters}, 每页大小: {api_page_size}", flush=True)
  1201. while iters < max_iters:
  1202. iters += 1
  1203. print(f"\n[{self.platform_name}] ---------- 第 {iters} 次请求 (cursor={cursor}) ----------", flush=True)
  1204. resp = await fetch_notes_page(cursor)
  1205. # 打印调试信息
  1206. if resp and isinstance(resp, dict) and resp.get('_debug'):
  1207. debug_info = resp.get('_debug', {})
  1208. sign_result = debug_info.get('signResult', {})
  1209. print(f"[{self.platform_name}] 🔍 调试信息:", flush=True)
  1210. print(f"[{self.platform_name}] 签名可用: {sign_result.get('hasSign', False)}", flush=True)
  1211. if sign_result.get('x_s'):
  1212. print(f"[{self.platform_name}] X-S: {sign_result.get('x_s', '')}", flush=True)
  1213. if sign_result.get('x_t'):
  1214. print(f"[{self.platform_name}] X-T: {sign_result.get('x_t', '')}", flush=True)
  1215. if sign_result.get('error'):
  1216. print(f"[{self.platform_name}] 签名错误: {sign_result.get('error', '')}", flush=True)
  1217. print(f"[{self.platform_name}] HTTP 状态: {debug_info.get('status', 'N/A')} {debug_info.get('statusText', '')}", flush=True)
  1218. # 移除调试信息,避免影响后续处理
  1219. resp.pop('_debug', None)
  1220. if not resp or not isinstance(resp, dict):
  1221. print(f"[{self.platform_name}] ❌ 第 {iters} 次拉取无响应,cursor={cursor}", flush=True)
  1222. print(f"[{self.platform_name}] 响应类型: {type(resp)}, 响应内容: {str(resp)[:500]}", flush=True)
  1223. break
  1224. if not (resp.get('success') or resp.get('code') == 0) or not resp.get('data'):
  1225. error_msg = str(resp)[:500]
  1226. print(f"[{self.platform_name}] ❌ 拉取失败 cursor={cursor}", flush=True)
  1227. print(f"[{self.platform_name}] 响应详情: {error_msg}", flush=True)
  1228. print(f"[{self.platform_name}] success={resp.get('success')}, code={resp.get('code')}, has_data={bool(resp.get('data'))}", flush=True)
  1229. # 打印详细的错误信息
  1230. if resp.get('msg'):
  1231. print(f"[{self.platform_name}] 错误消息: {resp.get('msg')}", flush=True)
  1232. if resp.get('message'):
  1233. print(f"[{self.platform_name}] 错误消息: {resp.get('message')}", flush=True)
  1234. if resp.get('error'):
  1235. print(f"[{self.platform_name}] 错误: {resp.get('error')}", flush=True)
  1236. # 打印调试信息
  1237. if resp.get('_debug'):
  1238. debug_info = resp.get('_debug', {})
  1239. print(f"[{self.platform_name}] HTTP 状态: {debug_info.get('status', 'N/A')} {debug_info.get('statusText', '')}", flush=True)
  1240. sign_result = debug_info.get('signResult', {})
  1241. if sign_result.get('error'):
  1242. print(f"[{self.platform_name}] 签名错误: {sign_result.get('error')}", flush=True)
  1243. if iters == 1:
  1244. print(f"[{self.platform_name}] 第一次请求失败,切换到滚动模式", flush=True)
  1245. return await collect_by_scrolling()
  1246. break
  1247. data = resp.get('data', {}) or {}
  1248. notes = data.get('notes', []) or []
  1249. if not notes:
  1250. print(f"[{self.platform_name}] ⚠️ cursor={cursor} 无作品,停止分页", flush=True)
  1251. break
  1252. # 统计页数
  1253. page_count += 1
  1254. print(f"[{self.platform_name}] ✅ 第 {page_count} 页获取成功,本页作品数: {len(notes)}", flush=True)
  1255. tags = data.get('tags', []) or []
  1256. if tags:
  1257. preferred = 0
  1258. for tag in tags:
  1259. if tag.get('id') == 'special.note_time_desc':
  1260. preferred = tag.get('notes_count', 0) or tag.get('notesCount', 0) or tag.get('count', 0) or 0
  1261. break
  1262. if preferred:
  1263. total = max(total, int(preferred))
  1264. print(f"[{self.platform_name}] 📊 从 tags 获取总数: {total} (preferred)", flush=True)
  1265. else:
  1266. tag_total = max([int(t.get('notes_count', 0) or t.get('notesCount', 0) or t.get('count', 0) or 0) for t in tags] + [0])
  1267. total = max(total, tag_total)
  1268. if tag_total > 0:
  1269. print(f"[{self.platform_name}] 📊 从 tags 获取总数: {total}", flush=True)
  1270. if not total:
  1271. t2 = int(data.get('total', 0) or data.get('total_count', 0) or data.get('totalCount', 0) or 0)
  1272. if not t2 and isinstance(data.get('page', {}), dict):
  1273. t2 = int(data.get('page', {}).get('total', 0) or data.get('page', {}).get('totalCount', 0) or 0)
  1274. total = max(total, t2)
  1275. if t2 > 0:
  1276. print(f"[{self.platform_name}] 📊 从 data.total 获取总数: {total}", flush=True)
  1277. parsed = parse_notes(notes)
  1278. new_items = []
  1279. for w in parsed:
  1280. if w.work_id and w.work_id not in seen_ids:
  1281. seen_ids.add(w.work_id)
  1282. new_items.append(w)
  1283. works.extend(new_items)
  1284. print(f"[{self.platform_name}] 📈 累计统计: 本页新作品={len(new_items)}, 累计作品数={len(works)}, 声明总数={total}", flush=True)
  1285. if total and len(works) >= total:
  1286. print(f"[{self.platform_name}] ✅ 已获取全部作品 (累计={len(works)} >= 总数={total}),停止分页", flush=True)
  1287. break
  1288. if len(new_items) == 0:
  1289. print(f"[{self.platform_name}] ⚠️ 本页无新作品,停止分页", flush=True)
  1290. break
  1291. next_page = data.get('page', "")
  1292. old_cursor = cursor
  1293. if next_page == cursor:
  1294. next_page = ""
  1295. if next_page == -1 or str(next_page) == "-1":
  1296. next_page = ""
  1297. if next_page is None or next_page == "":
  1298. if isinstance(cursor, int):
  1299. cursor = cursor + 1
  1300. else:
  1301. cursor = len(works) // api_page_size
  1302. print(f"[{self.platform_name}] 🔄 下一页 cursor: {old_cursor} -> {cursor} (自动递增)", flush=True)
  1303. else:
  1304. cursor = next_page
  1305. print(f"[{self.platform_name}] 🔄 下一页 cursor: {old_cursor} -> {cursor} (API返回)", flush=True)
  1306. await asyncio.sleep(0.5)
  1307. # 移除请求监听器
  1308. try:
  1309. self.page.remove_listener("request", handle_request)
  1310. except Exception:
  1311. pass
  1312. print(f"\n[{self.platform_name}] ========== 分页完成 ==========", flush=True)
  1313. print(f"[{self.platform_name}] 📊 分页统计: 总请求次数={iters}, 成功获取页数={page_count}, 累计作品数={len(works)}, 声明总数={total}", flush=True)
  1314. if captured_requests:
  1315. print(f"[{self.platform_name}] 📡 捕获到 {len(captured_requests)} 个 API 请求", flush=True)
  1316. for i, req in enumerate(captured_requests[:3], 1): # 只显示前3个
  1317. print(f"[{self.platform_name}] 请求 {i}: {req['method']} {req['url']}", flush=True)
  1318. if 'x-s' in req['headers']:
  1319. print(f"[{self.platform_name}] X-S: {req['headers']['x-s'][:50]}...", flush=True)
  1320. if 'x-t' in req['headers']:
  1321. print(f"[{self.platform_name}] X-T: {req['headers']['x-t']}", flush=True)
  1322. print(f"[{self.platform_name}] ========================================\n", flush=True)
  1323. except Exception as e:
  1324. import traceback
  1325. error_trace = traceback.format_exc()
  1326. print(f"[{self.platform_name}] 发生异常: {e}", flush=True)
  1327. traceback.print_exc()
  1328. return WorksResult(
  1329. success=False,
  1330. platform=self.platform_name,
  1331. error=str(e),
  1332. debug_info=f"异常详情: {error_trace[:500]}"
  1333. )
  1334. finally:
  1335. await self.close_browser()
  1336. debug_info = f"总请求次数={iters}, 成功获取页数={page_count}, 累计作品数={len(works)}, 声明总数={total}"
  1337. if len(works) == 0:
  1338. debug_info += " | 警告: 没有获取到任何作品,可能原因: Cookie失效、API调用失败、或账号无作品"
  1339. return WorksResult(
  1340. success=True,
  1341. platform=self.platform_name,
  1342. works=works,
  1343. total=total or len(works),
  1344. has_more=False,
  1345. next_page=-1,
  1346. debug_info=debug_info
  1347. )
  1348. async def get_comments(self, cookies: str, work_id: str, cursor: str = "") -> CommentsResult:
  1349. """
  1350. 获取账号下所有作品的评论 —— 完全复刻 get_xiaohongshu_work_comments.py 的7步流程。
  1351. """
  1352. all_comments: List[CommentItem] = []
  1353. total_comments = 0
  1354. has_more = False
  1355. browser = None
  1356. print(222222222222222222222222222222222222)
  1357. print(work_id)
  1358. try:
  1359. # --- Step 1: 初始化浏览器和 Cookie ---
  1360. cookie_list = self.parse_cookies(cookies)
  1361. playwright = await async_playwright().start()
  1362. browser = await playwright.chromium.launch(headless=False)
  1363. context = await browser.new_context(
  1364. viewport={"width": 1400, "height": 900},
  1365. user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  1366. )
  1367. await context.add_cookies(cookie_list)
  1368. page = await context.new_page()
  1369. # --- Step 2: 打开小红书主页 ---
  1370. await page.goto("https://www.xiaohongshu.com", wait_until="domcontentloaded")
  1371. await asyncio.sleep(1.5)
  1372. # --- Step 3: 检查并处理登录弹窗 ---
  1373. try:
  1374. if await page.is_visible(".login-container", timeout=3000):
  1375. await page.wait_for_selector(".login-container", state="hidden", timeout=120000)
  1376. except Exception as e:
  1377. pass # 忽略超时,继续执行
  1378. # --- 提取 User ID ---
  1379. user_id = None
  1380. for cookie in cookie_list:
  1381. if cookie.get('name') == 'x-user-id-creator.xiaohongshu.com':
  1382. user_id = cookie.get('value')
  1383. break
  1384. if not user_id:
  1385. raise ValueError("无法从 Cookie 中提取 user_id")
  1386. # --- Step 4: 跳转到用户主页 ---
  1387. profile_url = f"https://www.xiaohongshu.com/user/profile/{user_id}"
  1388. await page.goto(profile_url, wait_until="domcontentloaded")
  1389. await asyncio.sleep(2)
  1390. # --- 等待笔记区域加载 ---
  1391. try:
  1392. await page.wait_for_selector("#userPostedFeeds .note-item", timeout=20000)
  1393. except:
  1394. raise Exception("笔记区域未加载,请检查账号是否公开或 Cookie 是否有效")
  1395. # --- Step 5: 滚动到底部加载全部笔记 ---
  1396. last_height = None
  1397. while True:
  1398. await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
  1399. await asyncio.sleep(2)
  1400. new_height = await page.evaluate("document.body.scrollHeight")
  1401. if new_height == last_height:
  1402. break
  1403. last_height = new_height
  1404. # --- 获取所有封面图 ---
  1405. note_imgs = await page.query_selector_all("#userPostedFeeds .note-item .cover img")
  1406. print(f"共找到 {len(note_imgs)} 张封面图")
  1407. # --- Step 6 & 7: 依次点击封面图,捕获评论并结构化 ---
  1408. for i, img in enumerate(note_imgs):
  1409. try:
  1410. await img.scroll_into_view_if_needed()
  1411. await asyncio.sleep(0.5)
  1412. comment_resp = None
  1413. def handle_response(response):
  1414. nonlocal comment_resp
  1415. if "edith.xiaohongshu.com/api/sns/web/v2/comment/page" in response.url:
  1416. comment_resp = response
  1417. page.on("response", handle_response)
  1418. await img.click()
  1419. await asyncio.sleep(1.5)
  1420. page.remove_listener("response", handle_response)
  1421. if not comment_resp:
  1422. await page.keyboard.press("Escape")
  1423. continue
  1424. json_data = await comment_resp.json()
  1425. if not (json_data.get("success") or json_data.get("code") == 0):
  1426. await page.keyboard.press("Escape")
  1427. continue
  1428. data = json_data.get("data", {})
  1429. raw_comments = data.get("comments", [])
  1430. note_id = data.get("note_id", "")
  1431. for main_cmt in raw_comments:
  1432. # 主评论
  1433. user_info = main_cmt.get("user_info", {})
  1434. all_comments.append(CommentItem(
  1435. comment_id=main_cmt["id"],
  1436. parent_comment_id=None,
  1437. work_id=work_id,
  1438. content=main_cmt["content"],
  1439. author_id=user_info.get("user_id", ""),
  1440. author_name=user_info.get("nickname", ""),
  1441. author_avatar=user_info.get("image", ""),
  1442. like_count=int(main_cmt.get("like_count", 0)),
  1443. reply_count=main_cmt.get("sub_comment_count", 0),
  1444. create_time=self._timestamp_to_readable(main_cmt.get("create_time", 0)),
  1445. ))
  1446. # 子评论
  1447. for sub_cmt in main_cmt.get("sub_comments", []):
  1448. sub_user = sub_cmt.get("user_info", {})
  1449. all_comments.append(CommentItem(
  1450. comment_id=sub_cmt["id"],
  1451. parent_comment_id=main_cmt["id"],
  1452. work_id=work_id,
  1453. content=sub_cmt["content"],
  1454. author_id=sub_user.get("user_id", ""),
  1455. author_name=sub_user.get("nickname", ""),
  1456. author_avatar=sub_user.get("image", ""),
  1457. like_count=int(sub_cmt.get("like_count", 0)),
  1458. reply_count=0,
  1459. create_time=self._timestamp_to_readable(sub_cmt.get("create_time", 0)),
  1460. ))
  1461. # 关闭弹窗
  1462. await page.keyboard.press("Escape")
  1463. await asyncio.sleep(1)
  1464. except Exception as e:
  1465. # 出错也尝试关闭弹窗
  1466. try:
  1467. await page.keyboard.press("Escape")
  1468. await asyncio.sleep(0.5)
  1469. except:
  1470. pass
  1471. continue
  1472. # --- 返回结果 ---
  1473. total_comments = len(all_comments)
  1474. # return {
  1475. # 'success': True,
  1476. # 'platform': self.platform_name,
  1477. # 'work_comments': all_comments, # 注意:此处为扁平列表,如需按作品分组可在外层处理
  1478. # 'total': total_comments
  1479. # }
  1480. return CommentsResult(
  1481. success=True,
  1482. platform=self.platform_name,
  1483. work_id=work_id,
  1484. comments=all_comments,
  1485. total=total_comments,
  1486. has_more=has_more
  1487. )
  1488. except Exception as e:
  1489. import traceback
  1490. traceback.print_exc()
  1491. return CommentsResult(
  1492. success=True,
  1493. platform=self.platform_name,
  1494. work_id=work_id,
  1495. total=0
  1496. )
  1497. finally:
  1498. if browser:
  1499. await browser.close()
  1500. def _timestamp_to_readable(self, ts_ms: int) -> str:
  1501. """将毫秒时间戳转换为可读格式"""
  1502. from datetime import datetime
  1503. if not ts_ms:
  1504. return ""
  1505. try:
  1506. return datetime.fromtimestamp(ts_ms / 1000).strftime("%Y-%m-%d %H:%M:%S")
  1507. except Exception:
  1508. return ""
  1509. async def get_all_comments(self, cookies: str) -> dict:
  1510. """获取所有作品的评论 - 通过评论管理页面"""
  1511. print(f"\n{'='*60}")
  1512. print(f"[{self.platform_name}] 获取所有作品评论")
  1513. print(f"{'='*60}")
  1514. all_work_comments = []
  1515. captured_comments = []
  1516. captured_notes = {} # note_id -> note_info
  1517. try:
  1518. await self.init_browser()
  1519. cookie_list = self.parse_cookies(cookies)
  1520. await self.set_cookies(cookie_list)
  1521. if not self.page:
  1522. raise Exception("Page not initialized")
  1523. # 设置 API 响应监听器
  1524. async def handle_response(response):
  1525. nonlocal captured_comments, captured_notes
  1526. url = response.url
  1527. try:
  1528. # 监听评论列表 API - 多种格式
  1529. if '/comment/' in url and ('page' in url or 'list' in url):
  1530. json_data = await response.json()
  1531. print(f"[{self.platform_name}] 捕获到评论 API: {url[:100]}...", flush=True)
  1532. if json_data.get('success') or json_data.get('code') == 0:
  1533. data = json_data.get('data', {})
  1534. comments = data.get('comments', []) or data.get('list', [])
  1535. # 从 URL 中提取 note_id
  1536. import re
  1537. note_id_match = re.search(r'note_id=([^&]+)', url)
  1538. note_id = note_id_match.group(1) if note_id_match else ''
  1539. if comments:
  1540. for comment in comments:
  1541. # 添加 note_id 到评论中
  1542. if note_id and 'note_id' not in comment:
  1543. comment['note_id'] = note_id
  1544. captured_comments.append(comment)
  1545. print(f"[{self.platform_name}] 捕获到 {len(comments)} 条评论 (note_id={note_id}),总计: {len(captured_comments)}", flush=True)
  1546. # 监听笔记列表 API
  1547. if '/note/' in url and ('list' in url or 'posted' in url or 'manager' in url):
  1548. json_data = await response.json()
  1549. if json_data.get('success') or json_data.get('code') == 0:
  1550. data = json_data.get('data', {})
  1551. notes = data.get('notes', []) or data.get('list', [])
  1552. print(f"[{self.platform_name}] 捕获到笔记列表 API: {len(notes)} 个笔记", flush=True)
  1553. for note in notes:
  1554. note_id = note.get('note_id', '') or note.get('id', '')
  1555. if note_id:
  1556. cover_url = ''
  1557. cover = note.get('cover', {})
  1558. if isinstance(cover, dict):
  1559. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1560. elif isinstance(cover, str):
  1561. cover_url = cover
  1562. captured_notes[note_id] = {
  1563. 'title': note.get('title', '') or note.get('display_title', ''),
  1564. 'cover': cover_url,
  1565. }
  1566. except Exception as e:
  1567. print(f"[{self.platform_name}] 解析响应失败: {e}", flush=True)
  1568. self.page.on('response', handle_response)
  1569. print(f"[{self.platform_name}] 已注册 API 响应监听器", flush=True)
  1570. # 访问评论管理页面
  1571. print(f"[{self.platform_name}] 访问评论管理页面...", flush=True)
  1572. await self.page.goto("https://creator.xiaohongshu.com/creator/comment", wait_until="domcontentloaded", timeout=30000)
  1573. await asyncio.sleep(5)
  1574. # 检查登录状态
  1575. current_url = self.page.url
  1576. if "login" in current_url:
  1577. raise Exception("Cookie 已过期,请重新登录")
  1578. print(f"[{self.platform_name}] 页面加载完成,当前捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1579. # 滚动加载更多评论
  1580. for i in range(5):
  1581. await self.page.evaluate('window.scrollBy(0, 500)')
  1582. await asyncio.sleep(1)
  1583. await asyncio.sleep(3)
  1584. # 移除监听器
  1585. self.page.remove_listener('response', handle_response)
  1586. print(f"[{self.platform_name}] 最终捕获: {len(captured_comments)} 条评论, {len(captured_notes)} 个笔记", flush=True)
  1587. # 按作品分组评论
  1588. work_comments_map = {} # note_id -> work_comments
  1589. for comment in captured_comments:
  1590. # 获取笔记信息
  1591. note_info = comment.get('note_info', {}) or comment.get('note', {})
  1592. note_id = comment.get('note_id', '') or note_info.get('note_id', '') or note_info.get('id', '')
  1593. if not note_id:
  1594. continue
  1595. if note_id not in work_comments_map:
  1596. saved_note = captured_notes.get(note_id, {})
  1597. cover_url = ''
  1598. cover = note_info.get('cover', {})
  1599. if isinstance(cover, dict):
  1600. cover_url = cover.get('url', '') or cover.get('url_default', '')
  1601. elif isinstance(cover, str):
  1602. cover_url = cover
  1603. if not cover_url:
  1604. cover_url = saved_note.get('cover', '')
  1605. work_comments_map[note_id] = {
  1606. 'work_id': note_id,
  1607. 'title': note_info.get('title', '') or note_info.get('display_title', '') or saved_note.get('title', ''),
  1608. 'cover_url': cover_url,
  1609. 'comments': []
  1610. }
  1611. cid = comment.get('id', '') or comment.get('comment_id', '')
  1612. if not cid:
  1613. continue
  1614. user_info = comment.get('user_info', {}) or comment.get('user', {})
  1615. work_comments_map[note_id]['comments'].append({
  1616. 'comment_id': cid,
  1617. 'author_id': user_info.get('user_id', '') or user_info.get('id', ''),
  1618. 'author_name': user_info.get('nickname', '') or user_info.get('name', ''),
  1619. 'author_avatar': user_info.get('image', '') or user_info.get('avatar', ''),
  1620. 'content': comment.get('content', ''),
  1621. 'like_count': comment.get('like_count', 0),
  1622. 'create_time': comment.get('create_time', ''),
  1623. })
  1624. all_work_comments = list(work_comments_map.values())
  1625. total_comments = sum(len(w['comments']) for w in all_work_comments)
  1626. print(f"[{self.platform_name}] 获取到 {len(all_work_comments)} 个作品的 {total_comments} 条评论", flush=True)
  1627. except Exception as e:
  1628. import traceback
  1629. traceback.print_exc()
  1630. return {
  1631. 'success': False,
  1632. 'platform': self.platform_name,
  1633. 'error': str(e),
  1634. 'work_comments': []
  1635. }
  1636. finally:
  1637. await self.close_browser()
  1638. return {
  1639. 'success': True,
  1640. 'platform': self.platform_name,
  1641. 'work_comments': all_work_comments,
  1642. 'total': len(all_work_comments)
  1643. }