File size: 2,285 Bytes
7cb127d
8691822
f57973f
7cb127d
4d14585
a3b820d
f57973f
 
 
 
a3b820d
7cb127d
8691822
 
 
 
 
 
 
 
 
 
 
7cb127d
f57973f
8691822
f57973f
 
a3b820d
8691822
f57973f
8691822
f57973f
 
 
 
 
 
 
8691822
 
 
f57973f
 
8691822
a3b820d
f57973f
 
 
7cb127d
8691822
a3b820d
8691822
 
a3b820d
7cb127d
8691822
f57973f
8691822
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from cloakbrowser import launch_persistent_context
import time
import random

PROFILE_DIR = "/app/zo_profile"
HEADLESS = True          # True:无头运行,False:有界面(按回车关闭)
TARGET_URL = "https://666878.zo.computer/_boot"
MAX_RETRIES = 3
RETRY_DELAY_BASE = 2
PAGE_LOAD_TIMEOUT = 60000
SCREENSHOT_ON_SUCCESS = False

def main():
    context = None
    page = None
    try:
        print("正在启动浏览器...")
        context = launch_persistent_context(
            PROFILE_DIR,
            headless=HEADLESS,
            args=['--no-sandbox']
        )
        page = context.new_page()

        for attempt in range(1, MAX_RETRIES + 1):
            try:
                print(f"尝试访问 ({attempt}/{MAX_RETRIES}): {TARGET_URL}")
                page.goto(TARGET_URL, wait_until="load", timeout=PAGE_LOAD_TIMEOUT)

                page.wait_for_selector("body", timeout=5000)

                print(f"页面加载成功,当前 URL: {page.url}")
                print(f"页面标题: {page.title()}")

                if SCREENSHOT_ON_SUCCESS:
                    screenshot_path = f"screenshot_attempt_{attempt}.png"
                    page.screenshot(path=screenshot_path)
                    print(f"已保存截图: {screenshot_path}")

                break
            except Exception as e:
                print(f"第 {attempt} 次访问失败: {e}")
                if attempt == MAX_RETRIES:
                    print("所有重试均失败,脚本终止。")
                    return

                delay = RETRY_DELAY_BASE ** attempt + random.uniform(0, 1)
                print(f"等待 {delay:.1f} 秒后重试...")
                time.sleep(delay)

        if HEADLESS:
            print("Headless模式,等待 10 秒后自动关闭...")
            time.sleep(10)
        else:
            input("非Headless模式,按回车键关闭浏览器...")

    except Exception as e:
        print(f"脚本运行发生未捕获异常: {e}")
    finally:
        if context:
            print("正在关闭浏览器...")
            try:
                context.close()
            except Exception as e:
                print(f"关闭浏览器时出错: {e}")
        print("脚本结束。")

if __name__ == "__main__":
    main()