fix excepiton
This commit is contained in:
parent
8f0b81ebd9
commit
ea8632bfd6
36
app.py
36
app.py
@ -96,7 +96,27 @@ async def setCookie(a1: str) -> Dict[str, Any]:
|
||||
print(f"Error during setCookie operation: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
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)
|
||||
print("浏览器已重新加载")
|
||||
except Exception as e:
|
||||
print(f"Error during reload_browser operation: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dict[str, Any]:
|
||||
global browser_context, context_page # 声明全局变量
|
||||
global global_a1
|
||||
@ -131,11 +151,7 @@ async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dic
|
||||
if "Target crashed" in str(e):
|
||||
print("页面崩溃,正在重新初始化浏览器上下文和页面...")
|
||||
try:
|
||||
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 reload_browser()
|
||||
# 重试签名操作
|
||||
return await sign(uri, data, a1, web_session)
|
||||
except Exception as reinit_error:
|
||||
@ -143,7 +159,13 @@ async def sign(uri: str, data: Dict[str, Any], a1: str, web_session: str) -> Dic
|
||||
raise HTTPException(status_code=500, detail="Failed to recover from page crash")
|
||||
else:
|
||||
print(f"Error during sign operation: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
try:
|
||||
await reload_browser()
|
||||
# 重试签名操作
|
||||
return await sign(uri, data, a1, web_session)
|
||||
except Exception as reinit_error:
|
||||
print(f"重新初始化失败: {reinit_error}")
|
||||
raise HTTPException(status_code=500, detail="Failed to recover from page crash")
|
||||
|
||||
|
||||
@app.post("/sign")
|
||||
|
Loading…
x
Reference in New Issue
Block a user