mac/win使用pyinstaller打包app/exe文件,活着执行脚本,双击运行

🌸 踩坑记录

  • python环境最好使用虚拟环境,推荐使用conda管理,并且若本地有python环境,不要使用和 本地环境版本 相同 的虚拟环境
    • 这里踩坑较多,已经记不清楚注意点
      • 虚拟环境
      • python版本不要和本地环境一样
  • mac/win只能打包相应的程序,mac对应app,win对应exe,也可使用终端/窗口还能看见日志。
  • mac压缩后传递可能出现软件损坏/开发者未受信任情况
    • 使用终端运行 sudo spctl --master-disable
    • 系统设置、安全与隐私、允许来自任何来源,勾选
  • 若有路径读取请使用os.path.dirname(sys.argv[0]),并且最好放在同一个文件夹下
python 复制代码
# 重点这行
path = os.path.dirname(sys.argv[0])
# 拼接路径
f = os.path.join(path, 'url.txt')
# 按行读取,形成列表
websites = open(f).readlines()
  • 记录打包命令
python 复制代码
"""
常用参数 含义
-i 或 -icon 生成icon
-F 创建一个绑定的可执行文件
-w 使用窗口,无控制台
-C 使用控制台,无窗口
-D 创建一个包含可执行文件的单文件夹包(默认情况下)
-n 文件名 

mac pyinstaller -F -w -i favicon.ico trustCertificates.py
win pyinstaller -F --onefile -i favicon.ico trustCertificates.py
"""


🌸 源码

python 复制代码
import os
import sys

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

"""
常用参数 含义
-i 或 -icon 生成icon
-F 创建一个绑定的可执行文件
-w 使用窗口,无控制台
-C 使用控制台,无窗口
-D 创建一个包含可执行文件的单文件夹包(默认情况下)
-n 文件名 

mac pyinstaller -F -w -i favicon.ico trustCertificates.py
win pyinstaller -F --onefile -i favicon.ico trustCertificates.py
"""

chrome_options = Options()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

# 创建Chrome驱动程序
chrome_driver_path = 'path_to_chromedriver'
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)

# 网站列表
path = os.path.dirname(sys.argv[0])
f = os.path.join(path, 'url.txt')

websites = open(f).readlines()

# 循环访问每个网站并信任证书
for website in websites[1:]:
    try:
        driver.get(website)
        # 等待直到页面加载完成
        WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
        print(f"成功信任证书: {website}")
    except Exception as e:
        raise e
        print(f"无法信任证书: {website}, 错误信息: {str(e)}")

driver.get(websites[0])

while True:
    pass
相关推荐
郭庆汝5 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
思则变8 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
漫谈网络8 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
try2find10 小时前
安装llama-cpp-python踩坑记
开发语言·python·llama
博观而约取11 小时前
Django ORM 1. 创建模型(Model)
数据库·python·django
精灵vector12 小时前
构建专家级SQL Agent交互
python·aigc·ai编程
Zonda要好好学习12 小时前
Python入门Day2
开发语言·python
Vertira12 小时前
pdf 合并 python实现(已解决)
前端·python·pdf
太凉12 小时前
Python之 sorted() 函数的基本语法
python
项目題供诗13 小时前
黑马python(二十四)
开发语言·python