This commit is contained in:
熊潇 2025-05-01 19:33:58 +08:00
parent 1627d3cf89
commit 837ae3e01b

View File

@ -12,6 +12,9 @@ monkey.patch_all()
app = Flask(__name__) app = Flask(__name__)
A1 = "" A1 = ""
# 确保在模块级别声明全局变量
browser_context = None
context_page = None
def get_context_page(instance, stealth_js_path): def get_context_page(instance, stealth_js_path):
@ -42,17 +45,17 @@ print("跳转小红书首页成功,等待调用")
def sign(uri, data, a1, web_session): def sign(uri, data, a1, web_session):
global browser_context, context_page # 声明全局变量
try: try:
# Ensure the page is still valid and reload if necessary # 确保页面仍然有效,如果页面已关闭则重新初始化
if context_page.is_closed(): if context_page is None or context_page.is_closed():
global browser_context, context_page
browser_context, context_page = get_context_page(playwright, stealth_js_path) browser_context, context_page = get_context_page(playwright, stealth_js_path)
context_page.goto("https://www.xiaohongshu.com") context_page.goto("https://www.xiaohongshu.com")
time.sleep(5) time.sleep(5)
context_page.reload() context_page.reload()
time.sleep(1) time.sleep(1)
# Evaluate the JavaScript function # 执行 JavaScript 函数
encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data]) encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data])
return { return {
"x-s": encrypt_params["X-s"], "x-s": encrypt_params["X-s"],