简体   繁体   中英

ValueError while running Python script from command line

I am trying to call a python script from the command line as follows:

python Browse_list.py "firefox" "https://www.n11.com/magaza/tozbitti?iw=motor"["&m=Aeg","&m=Arnica","&m=Bosch","&m=Electrolux","&m=Philips"] [1,1,1,1,1] [4,2,7,4,6] "&pg="

Here is the script file:

import sys
import selenium
from selenium import webdriver
from time import sleep
import os

if sys.argv[1] == 'Chrome':

    driver = webdriver.Chrome(executable_path=os.getcwd() + '\\chromedriver.exe')

elif sys.argv[1] == 'Firefox':

    driver = webdriver.Firefox(executable_path=os.getcwd() + '\\geckodriver.exe')

elif sys.argv[1] == 'Microsoft Edge':

    driver = webdriver.Edge(executable_path=os.getcwd() + '\\msedgedriver.exe')

elif sys.argv[1] == 'Opera':

    driver = webdriver.Opera(executable_path=os.getcwd() + '\\operadriver.exe')

n = len(sys.argv[3])
s = sys.argv[3][1:n-1] 
s = s.split(',') 

a = sys.argv[4][1:n-1] 
a = a.split(',') 
  
A = [int(i) for i in a]

b = sys.argv[5][1:n-1] 
b = b.split(',') 
  
B = [int(i) for i in b]

for j in range(0, len(A)): 

    for i in range(A[j], B[j]):

        driver.get(sys.argv[2]+s[j]+sys.argv[6]+str(i))

        sleep(4)

#driver.quit()

I get the following error:

    Traceback (most recent call last):
      File "Browse_list.py", line 30, in <module>
        A = [int(i) for i in a]
      File "Browse_list.py", line 30, in <listcomp>
        A = [int(i) for i in a]
ValueError: invalid literal for int() with base 10: '1,1,1,1,1]'

how to resolve and change the array of strings to array of ints? Thanks.

The problem is that you are not parsing data type because command line arguments are always passed as strings. You will need to parse them into your required data type

not correct

a = a.split(',') 
b = b.split(',') 

correct

a = map(int, a.strip('[]').split(','))
b = map(int, b.strip('[]').split(','))

so your final code looks like:

import sys
import selenium
from selenium import webdriver
from time import sleep
import os
from webdriver_manager.chrome import ChromeDriverManager

if sys.argv[1] == 'Chrome':

    driver = webdriver.Chrome(executable_path=os.getcwd() + '\\chromedriver.exe')

elif sys.argv[1] == 'Firefox':

    driver = webdriver.Firefox(executable_path=os.getcwd() + '\\geckodriver.exe')

elif sys.argv[1] == 'Microsoft Edge':

    driver = webdriver.Edge(executable_path=os.getcwd() + '\\msedgedriver.exe')

elif sys.argv[1] == 'Opera':

    driver = webdriver.Opera(executable_path=os.getcwd() + '\\operadriver.exe')

n = len(sys.argv[3])
s = sys.argv[3][1:n-1] 
s = s.split(',') 

a = sys.argv[4][1:n-1] 
a = map(int, a.strip('[]').split(','))
  
A = [int(i) for i in a]

b = sys.argv[5][1:n-1] 
b = map(int, b.strip('[]').split(','))
  
B = [int(i) for i in b]
driver = webdriver.Chrome(ChromeDriverManager().install())
for j in range(0, len(A)): 

    for i in range(A[j], B[j]):

        
        driver.get(sys.argv[2]+s[j]+sys.argv[6]+str(i))

        sleep(4)

#driver.quit()

Even though all arrays are of the same size I had to get the length of each array separately as in following:

n = len(sys.argv[3])
s = sys.argv[3][1:n-1] 
s = s.split(',') 

n = len(sys.argv[4])
a = sys.argv[4][1:n-1]
a = a.split(',') 
  
A = [int(i) for i in a]

n = len(sys.argv[5])
b = sys.argv[5][1:n-1] 
b = b.split(',') 

Maybe it differs how python gets each input which makes the length different so you to get the length each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM