Files
lm_code/test1.py
2025-10-31 11:33:34 +08:00

71 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import time
from DrissionPage import ChromiumPage, ChromiumOptions
# ========== 配置 ==========
API_BASE_URL = "http://127.0.0.1:50326"
headers = {
"Authorization": f"Bearer asp_174003986c9b0799677c5b2c1adb76e402735d753bc91a91",
"Content-Type": "application/json"
}
# # ========== 步骤 1: 创建浏览器环境 ==========
# print("1. 创建浏览器环境...")
# response = requests.post(
# f"{API_BASE_URL}/api/browser/create",
# headers=headers,
# params = {
# "current": 1,
# "pageSize": 20,
# "keyword": "test"
# }
# )
#
# env_id = response.json()["data"]["envId"]
# print(f" 环境创建成功ID: {env_id}")
# ========== 步骤 2: 启动浏览器 ==========
print("2. 启动浏览器...")
response = requests.post(
f"{API_BASE_URL}/api/browser/start",
json={"envId": 146473},
headers=headers
)
#
# debug_port = response.json()["data"]["port"]
# print(f" 浏览器已启动,调试端口: {debug_port}")
#
# # ========== 步骤 3: 连接 DrissionPage ==========
# print("3. 连接 DrissionPage...")
# time.sleep(3) # 等待浏览器完全启动
#
# # 方式1: 通过端口连接(推荐)
# co = ChromiumOptions().set_local_port(debug_port)
# page = ChromiumPage(addr_or_opts=co)
#
# # 方式2: 直接使用端口号
# # from DrissionPage import Chromium
# # page = Chromium(debug_port)
#
# # 方式3: 使用地址:端口
# # page = Chromium(f'127.0.0.1:{debug_port}')
#
# # 方式4: 使用 WebSocket URL需要先获取 ws_url
# # ws_url = response.json()["data"]["ws"]
# # page = Chromium(ws_url)
#
# print(" DrissionPage 连接成功")
#
# # ========== 步骤 4: 执行自动化操作 ==========
# print("4. 执行自动化操作...")
#
# # 访问网页
# page.get('https://www.example.com')
# print(f" 当前页面: {page.title}")
#
# # 更多操作示例
# page.get_screenshot('screenshot.png') # 截图
# print(" 已保存截图")
#
# print("\n完成")