You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为非技术用户打包含Selenium与chromedriver的Python爬虫应用?

Best Ways to Package Your Selenium Crawler with Chromedriver for Non-Technical Users

Great question—packaging Selenium apps with chromedriver can be tricky for non-technical users, but there are several reliable solutions that avoid manual file placement. Here are the best options:

PyInstaller is the go-to tool for bundling Python apps into single executables, and it handles binary assets like chromedriver seamlessly. Here's how to set it up:

  • Install PyInstaller

    pip install pyinstaller
    
  • Bundle Chromedriver with Your App
    First, place your chromedriver binary in the same folder as your main script. Then choose one of these methods:

    • Quick Command Line Method:
      Run this (use : instead of ; on macOS/Linux):

      pyinstaller --onefile --add-binary "chromedriver.exe;." your_script.py
      

      This tells PyInstaller to include chromedriver and place it in the same temporary directory as the executable when it runs.

    • Spec File for More Control:
      Generate a spec file first:

      pyinstaller your_script.py
      

      Open the generated your_script.spec file, find the datas section, and add chromedriver:

      datas = [('chromedriver.exe', '.')]  # Replace with 'chromedriver' for macOS/Linux
      

      Then build using the spec file:

      pyinstaller your_script.spec
      
  • Update Your Script to Locate Chromedriver
    PyInstaller extracts files to a temp folder when running the executable, so add this helper function to your script to find chromedriver automatically:

    import os
    import sys
    
    def get_chromedriver_path():
        if getattr(sys, 'frozen', False):
            # Running as bundled executable
            base_path = sys._MEIPASS
        else:
            # Running as regular script
            base_path = os.path.dirname(os.path.abspath(__file__))
        return os.path.join(base_path, 'chromedriver.exe')  # Use 'chromedriver' on macOS/Linux
    
    # Use it in your Selenium setup:
    from selenium import webdriver
    driver = webdriver.Chrome(executable_path=get_chromedriver_path())
    

2. cx_Freeze

cx_Freeze is another solid option for cross-platform packaging:

  • Install cx_Freeze

    pip install cx_Freeze
    
  • Create a setup.py File
    Place this in your project folder (adjust for your OS):

    import sys
    from cx_Freeze import setup, Executable
    
    base = 'Win32GUI' if sys.platform == 'win32' else None  # Use Win32GUI for GUI apps
    
    executables = [Executable('your_script.py', base=base)]
    include_files = ['chromedriver.exe']  # Replace with 'chromedriver' for macOS/Linux
    
    setup(
        name='YourCrawlerApp',
        version='1.0',
        description='Web crawler that exports to Excel',
        executables=executables,
        options={'build_exe': {'include_files': include_files}}
    )
    
  • Build the Executable

    python setup.py build
    
  • Adjust Script Pathing
    Add this function to your script to find chromedriver:

    import os
    import sys
    
    def get_chromedriver_path():
        if getattr(sys, 'frozen', False):
            base_path = os.path.dirname(sys.executable)
        else:
            base_path = os.path.dirname(os.path.abspath(__file__))
        return os.path.join(base_path, 'chromedriver.exe')
    

3. webdriver-manager (No Manual Bundling)

If you want to avoid bundling chromedriver entirely (and skip version compatibility issues), use webdriver-manager to auto-download the correct chromedriver version for the user's system on first run:

  • Install webdriver-manager

    pip install webdriver-manager
    
  • Update Your Selenium Setup
    Replace your existing chromedriver code with:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    

Then package the app with PyInstaller or cx_Freeze as usual—no need to include chromedriver manually.

Final Tips for Non-Technical Users

  • If using PyInstaller's --onefile option, users get a single .exe (or executable file) they can double-click to run.
  • Zip the output folder (for cx_Freeze) or single file (PyInstaller) to make sharing easy.

内容的提问来源于stack exchange,提问作者user2868900

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:07:50