简体   繁体   English

从命令行运行 Python 脚本时出现 ValueError

[英]ValueError while running Python script from command line

I am trying to call a python script from the command line as follows:我正在尝试从命令行调用 python 脚本,如下所示:

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=" python Browse_list.py "firefox" "https://www.n11.com/magaza/tozbitti?iw=motor"["&m=Aeg","&m=Arnica","&m=Bosch","&m=Electrolux" ,"&m=飞利浦"] [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.问题是您没有解析数据类型,因为命令行 arguments 始终作为字符串传递。 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:即使所有 arrays 的大小相同,我也必须分别获取每个数组的长度,如下所示:

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.也许 python 获取每个输入的方式不同,这使得长度不同,因此您每次都获取长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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