xiaohongshu.py 95 KB

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