This commit is contained in:
熊潇 2025-05-04 15:19:27 +08:00
parent e591d2cb7c
commit 4909143938

32
app.py
View File

@ -61,6 +61,16 @@ async def initialize_playwright():
await loginModal.evaluate("el => el.click()") await loginModal.evaluate("el => el.click()")
print("登录弹窗已关闭") print("登录弹窗已关闭")
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(): async def show_cookies():
global browser_context, context_page, global_a1 # 声明全局变量 global browser_context, context_page, global_a1 # 声明全局变量
cookies = await browser_context.cookies() cookies = await browser_context.cookies()
@ -106,21 +116,12 @@ async def reload_browser():
global browser_context, context_page # 声明全局变量 global browser_context, context_page # 声明全局变量
global playwright_instance global playwright_instance
try: try:
if context_page: await close_playwright()
await context_page.close() # 重新初始化 Playwright 和浏览器上下文
if browser_context: await initialize_playwright()
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)
print("浏览器已重新加载") print("浏览器已重新加载")
show_cookies() await show_cookies()
except Exception as e: except Exception as e:
print(f"Error during reload_browser operation: {e}") print(f"Error during reload_browser operation: {e}")
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
@ -136,9 +137,6 @@ async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dic
await context_page.reload() await context_page.reload()
await asyncio.sleep(1) await asyncio.sleep(1)
# if a1 != global_a1:
# await setCookie(a1) # Ensure setCookie is awaited properly
# 执行 JavaScript 函数 # 执行 JavaScript 函数
b1 = await context_page.evaluate("() => localStorage.getItem('b1')") b1 = await context_page.evaluate("() => localStorage.getItem('b1')")
b1b1 = await context_page.evaluate("() => localStorage.getItem('b1b1')") b1b1 = await context_page.evaluate("() => localStorage.getItem('b1b1')")