-1

python newbie here. I am trying to create an executable of my web scraping script so other users can access it, but I am having some problems. The script does include some inputs to work. I have tried using both auto-py-to-exe and pyinstaller, but after the exe is created in both, it doesn't seem to execute the script. For pyinstaller specifically I believe it has something to do with the .spec file, but I'm not sure.

I try to run this:

pyinstaller webscraper.spec webscraper.py

This is how my .spec file looks:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['webscraper.py'],
    pathex=['C:\\Users\\myname\\Python\\webscraper'],
    binaries=[('C:\\Program Files (x86)\\chromedriver.exe', '***.\\selenium\\webdriver**')],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='webscraper',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='webscraper',
)

That seems to create the folder in \dist but running it doesn't do anything, as in nothing comes up for any inputs or anything.

When using auto-py-to-exe I am able to get where you can actually input the parameters, but then it fails opening chrome and just closes, so I can't see any errors that may be popping up.

I'm just not exactly sure what I am doing wrong here. Let me know if I need to include more info.

Code:

import os
import time
import math

import pandas as pd
import win32com.client as win32
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# load Excel file into python
excel_folder = input("Input excel folder path: ")
os.chdir(excel_folder)
excel_file = input("Input excel filename (with extension): ")
sheet_name = input("Input sheet name: ")
part_column = input("Input part number column name: ")
price_column = "Vendor Price"
batch_column = "Batch"
qty_column = "Vendor QTY"
total_column = "Total Price"
totalqty_column = "Total QTY"
xldata = pd.read_excel(excel_file, sheet_name=sheet_name)

# chrome driver path
s = Service(r"C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=s)

# get total row count (index=None for rows without labels)
df = pd.DataFrame(xldata, index=None)
rows = len(df.axes[0])

# close Excel if the file is open
xapp = win32.DispatchEx("Excel.Application")
xl = win32.GetActiveObject("Excel.Application")
for wb in xl.Workbooks:
    if os.path.dirname(wb.FullName) == excel_folder:
        wb.Close(True)
xapp.Quit()

# loop through all part numbers in worksheet
for row_curser in range(0, rows):
    xl_value1 = xldata[part_column][row_curser]
    xl_value2 = xldata[totalqty_column][row_curser]

    # go to part number webpage
    page = r"https://www.mcmaster.com/" + xl_value1 + r'/'
    driver.get(page)
    driver.maximize_window()

    # wait for page to load before locating elements
    time.sleep(1)

    # locate price
    price = driver.find_element(By.CLASS_NAME, "PrceTxt")

    # split price and UoM
    pt = price.text
    s = pt.split(' ')
    cost = s[0]
    batch = ' '.join(s[1:])
    qty = s[-1]

    # transform each to qty of 1
    if qty == 'Each':
        qty = 1
    else:
        qty = qty

    # get total price
    costsplit = cost.split('$')
    total = math.ceil(float(xl_value2) / float(qty)) * float(costsplit[1])

    # write prices to Excel
    df.at[row_curser, price_column] = cost
    df.at[row_curser, batch_column] = batch
    df.at[row_curser, qty_column] = qty
    df.at[row_curser, total_column] = total
    df.to_excel(excel_file, index=False)

# close web browser
driver.quit()

# open Excel file
xlapp = win32.DispatchEx("Excel.Application")
xlapp.WindowState = -4137
xlapp.Visible = 1
wb = xlapp.workbooks.open(excel_folder + "\\" + excel_file)
scorbin
  • 9
  • 5
  • can you post your code? – Alexander Sep 03 '22 at 05:41
  • Yes, of course. Added. – scorbin Sep 06 '22 at 15:07
  • Add logging module to your code, wrap some functions and calls with try/except so you get informed about whats wrong. Open the executable from terminal or add FileHandler to your logging, so you can retain text of the failure. – m3nda Sep 06 '22 at 16:55
  • Will that help with the failure of the executable? Running the program from PyCharm, the program runs just as intended. It's only when converting to the exe, that the program doesn't fully run. It asks for the inputs, but it fails at opening chrome. Also not entirely sure what FileHandler is. – scorbin Sep 06 '22 at 17:15

2 Answers2

0

try to create the exe with the following command

python -m pyinstaller --onefile webscraper.py

webscraper.exe will be created in the dist folder of the same directory where your webscraper.py file is located. Please make sure to put chromedriver in the location where you have entered in the code. For example In the webscraper.py if you set the executable path to be in the same directory as of your project files, you'll need to copy the chromedriver in the dist directory too where your webscraper.exe is. Also, if you are opening any files in the webscraper.py code, you'll need to put those files too in the dist folder. If still issue doesn't resolve, Please share your code or else run the executable file in cmd, It won't close and you'll see the error.

I hope this somehow helps. Thank You!

  • Still, If your issue doesn't get resolved Please share the code or maybe just the error you see when you run the exe in cmd. – Hassan Tariq Sep 03 '22 at 10:05
  • Code added. I tried running that but it said there was no module named pyinstaller. It seems to only run if I don't include the "python -m" section. When I am able to run the exe, the cmd window closes too quickly, so don't see any errors if there are any. Using pysintaller, it says the build was successful. – scorbin Sep 06 '22 at 15:07
  • Please share your code or else run the executable file in cmd, It won't close and you'll see the error. Case you use Windows, open CMD prior to launch. Anyway, I insist, add logging feature to your coding. – m3nda Sep 06 '22 at 16:57
  • Code as been added to the main body of the post. I guess I am not sure what should be happening when running the exe in cmd. When I do that via "start webscraper.exe", I don't see any errors. Nothing comes up in cmd when the program terminates. Also not sure how to add logging to my code (sorry, I am really new to python) – scorbin Sep 06 '22 at 17:14
  • OK, I ran it in cmd by omitting "start" and I am seeing this error: Failed to execute script 'webscraper' due to unhandled exception! It also says at the top: Message: session not created: This version of ChromeDriver only supports Chrome version 103. That seems strange since the program executes just fine in PyCharm. – scorbin Sep 06 '22 at 17:22
  • No issue, please check your chrome version from chrome settings and just update the chromedriver – Hassan Tariq Sep 11 '22 at 01:28
0

Thanks everyone. Seeing the error when running the exe in cmd per @m3nda, I was able to see that my chromedriver just needed to be updated in order for the exe to actually work, despite it working PyCharm.

scorbin
  • 9
  • 5