Python 尝试替换字符串中的字符并为其分配新变量,然后将其用作参数时出错
当尝试替换字符串中的字符并为其分配新的字符时,有人可以帮助我修复此错误吗 变量,然后将其用作参数并获得以下错误: 总的来说, ping(域) TypeError:“str”对象不可调用Python 尝试替换字符串中的字符并为其分配新变量,然后将其用作参数时出错,python,typeerror,Python,Typeerror,当尝试替换字符串中的字符并为其分配新的字符时,有人可以帮助我修复此错误吗 变量,然后将其用作参数并获得以下错误: 总的来说, ping(域) TypeError:“str”对象不可调用 from selenium import webdriver from selenium.webdriver.common.keys import Keys import platform import subprocess class pingStats: packets = ''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import platform
import subprocess
class pingStats:
packets = ''
def Main():
url = input("Enter url: ")
urlArray = [url]
path = 'C://Users//me//Downloads//ChromeDriver88//chromedriver.exe'
bot1 = webdriver.Chrome(executable_path=path)
bot1.get(url)
pack_user = pingStats
ping = input('')
packetsUser = input('')
pack_user.packets = packetsUser
if ping == 'ping':
#where the error is happening i think
domain = url.replace('https://', '')
ping(domain)
#where the error is happening i think
else:
print('no command named ' + ping)
def ping(host):
packets = pingStats
param = '-n' if platform.system().lower()=='windows' else '-c'
command = ['ping', param, pingStats.packets, host]
return subprocess.call(command) == 0
if __name__ == '__main__':
Main()
问题在于对不同的对象使用相同的名称
ping
ping
这里表示由input()
返回的str
对象:
您还定义了一个名为ping()
的函数:
直接的解决方法是更改为input()
定义的对象的名称
例如,使用:
userInput = input('')
并在所有位置使用userInput
,而不是:
ping = input('')
始终以完全回溯的方式发布整个错误消息。
userInput = input('')
ping = input('')