From 4909143938226ea7a36dbbf2b5e4e75501cd2f3f Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sun, 4 May 2025 15:19:27 +0800 Subject: [PATCH] up --- app.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index 657255b..7a9fb2c 100644 --- a/app.py +++ b/app.py @@ -61,6 +61,16 @@ async def initialize_playwright(): await loginModal.evaluate("el => el.click()") print("登录弹窗已关闭") print("跳转小红书首页成功,等待调用") +async def close_playwright(): + global playwright_instance, browser_context, context_page + if context_page: + await context_page.close() + if browser_context: + await browser_context.close() + if playwright_instance: + await playwright_instance.stop() + print("Playwright 已关闭") + async def show_cookies(): global browser_context, context_page, global_a1 # 声明全局变量 cookies = await browser_context.cookies() @@ -106,21 +116,12 @@ async def reload_browser(): global browser_context, context_page # 声明全局变量 global playwright_instance try: - if context_page: - await context_page.close() - if browser_context: - await browser_context.close() - if playwright_instance: - await playwright_instance.stop() - # 重新初始化 Playwright - playwright_instance = await async_playwright().start() - browser_context, context_page = await get_context_page(playwright_instance, "stealth.min.js") - await context_page.goto("https://www.xiaohongshu.com") - await asyncio.sleep(5) - await context_page.reload() - await asyncio.sleep(1) + await close_playwright() + # 重新初始化 Playwright 和浏览器上下文 + await initialize_playwright() + # 重新加载页面 print("浏览器已重新加载") - show_cookies() + await show_cookies() except Exception as e: print(f"Error during reload_browser operation: {e}") raise HTTPException(status_code=500, detail=str(e)) @@ -135,9 +136,6 @@ async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dic await asyncio.sleep(5) await context_page.reload() await asyncio.sleep(1) - - # if a1 != global_a1: - # await setCookie(a1) # Ensure setCookie is awaited properly # 执行 JavaScript 函数 b1 = await context_page.evaluate("() => localStorage.getItem('b1')")