Files
aiData/Tools/T3_GetCookie.py
HuangHai 90870194d0 'commit'
2026-01-12 08:10:18 +08:00

65 lines
2.7 KiB
Python
Raw Permalink 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 os
import sys
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# pip install selenium
# 将项目根目录添加到系统路径,确保可以导入 Util 模块
# 注意PaChongGaoDeKit 位于 Util 目录下
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, '..'))
from Util.PaChongGaoDeKit import PaChongGaoDeKit
def run_login_tool(force=False):
"""
运行高德地图 Cookie 获取工具。
:param force: 如果为 True则跳过 'Cookie 已存在' 的询问,直接开始流程。
:return: Boolean, True 表示获取成功, False 表示失败或取消。
"""
logger.info("正在启动高德地图 Cookie 获取工具,使用手机号18686619970+验证码登录即可!")
# 默认将 Cookie 保存到项目根目录下的 Json/amap_cookies.json
cookie_path = os.path.join(current_dir, '../Json/amap_cookies.json')
# 检查 Cookie 文件是否已存在
if os.path.exists(cookie_path) and not force:
logger.info(f"提示:检测到已存在 Cookie 文件: {os.path.abspath(cookie_path)}")
logger.info("如果您本地的 Cookie 文件没有过期且可以正常使用,则不需要重新生成。")
user_choice = input("是否仍要继续运行并重新获取 Cookie(y/n): ").strip().lower()
if user_choice != 'y':
logger.info("用户取消操作,程序退出。")
return False
logger.info("\n" + "-"*60)
logger.info("即将开始获取 Cookie请按照以下步骤操作")
logger.info("1. 程序将自动打开一个 Chrome 浏览器窗口。")
logger.info("2. 请在该窗口中完成高德地图账号登录(扫码或账号密码)。")
logger.info("3. 登录成功后,保持页面处于控制台或个人中心状态。")
logger.info("4. 回到此终端窗口,按下回车键,程序将保存 Cookie 并自动关闭浏览器。")
logger.info("-"*60 + "\n")
# 初始化工具类,强制开启非无头模式(显示浏览器窗口)以便手动登录
# 确保 Json 目录存在
os.makedirs(os.path.dirname(cookie_path), exist_ok=True)
kit = PaChongGaoDeKit(headless=False, cookie_file=cookie_path)
try:
# 执行登录并保存流程
return kit.login_and_save_cookies()
except Exception as e:
logger.error(f"执行过程中发生意外错误: {e}")
return False
finally:
# 关闭浏览器资源
kit.close()
logger.info("工具运行结束。")
if __name__ == "__main__":
run_login_tool()