亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 pyinstaller 將 chromedriver.exe 與在

如何使用 pyinstaller 將 chromedriver.exe 與在

慕仙森 2023-08-15 17:04:49
據我所知,將chromedriver.exe與在 selenium webdriver 上運行的 python 腳本合并或添加。這是我的代碼(python):# importing packages / modulesimport osimport service? # This module cannot be installed as it asks for Microsoft Visual C++ 14 to be isntalled on pcfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Options# Defining required functions that needs to be used again and againdef init_driver():? ? chrome_path = os.path.join(os.path.dirname(__file__), 'selenium','webdriver','chromedriver.exe')? ? service = service.Service(chrome_path)? # So, this cannot be used to merge it.? ? path = webdriver.Chrome(service, options=Options())driver = init_driver()以及 cmd 中給出的命令:pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

我們需要牢記以下兩件事:


pyinstaller 選項

源代碼中驅動程序的路徑

Chromedriver.exe 是一個二進制文件,因此,我們可以使用--add binary我在問題中提到的方式將其添加到 Pyinstaller 中。因此,我們在 cmd 中的 pyinstaller 命令將是這樣的:


pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"

其次,我們需要以這樣的方式修改源代碼,使其既不會損害Python控制臺中的代碼,也不會損害轉換后的可執行文件中的代碼。因此,我們需要將driver_path代碼更改為


driver = webdriver.Chrome('path/to/chromedriver.exe', options=Options())

像這樣的事情


import os

import sys


def resource_path(another_way):

    try:

        usual_way = sys._MEIPASS  # When in .exe, this code is executed, that enters temporary directory that is created automatically during runtime.

    except Exception:

        usual_way = os.path.dirname(__file__)  # When the code in run from python console, it runs through this exception.

    return os.path.join(usual_way, another_way)



driver = webdriver.Chrome(resource_path('./driver/chromedriver.exe'), options=Options())

我們編寫該Exception部分是為了使其能夠與 python 控制臺一起工作。如果僅以 .exe 格式運行是主要目標,那么我們也可以忽略 Exception 部分。


當我們嘗試從類中_MEIPASS訪問受保護的成員時,此代碼會在 python 控制臺中拋出警告。除此之外,突出顯示sys.pyi的是找不到引用。_MEIPASS


查看完整回答
反對 回復 2023-08-15
  • 1 回答
  • 0 關注
  • 270 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號