URL 的循环代码无法正常工作

Loop code for URL is not working properly

所以我的检查代码应该没问题...

URL = "https://youtube.com/testingforthis"
s1 = url.startswith('https://')
s2 = url.startswith('http://')
if s1 == True or s2 == True:
  na = [':', '/', '.']
  for i in na:
    furl = url.replace(i, '')
  alphacheck = furl.isalnum()
  if alphacheck == True:
    ng = False
  else:
    ng = True
else:
  ng = True

但由于某些原因这个循环仍在激活

if ng == True:
  print(url, "is not valid")
  exit(0)

如果您 运行 代码,它会崩溃。为什么?

在此循环中:

  for i in na:
        furl = url.replace(i, '')

您继续替换原始 URL,而不是包含之前替换的 URL。所以furl的最终值只是有最后一个替换,而不是所有的替换。

您应该将替换结果赋值回同一个变量。

furl = url
for i in na:
    furl = furl.replace(i, '')