您如何将列表放入网络浏览器链接中?

How do you put lists in webbrowser links?

我正在尝试创建一个可以在您的浏览器中随机打开 Bitly link 的程序。

import random
import webbrowser

#set the length of the url
length = random.randint(1,7)

#list of all possible characters in the key
characters = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h',
 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P',
  'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y',
   'Y', 'z', 'Z',]

#create the string of text on the end of the link
key = []
for x in range (1, length):
    key.append(random.choice(characters))

webbrowser.open_new_tab('https://bit.ly/', *key, sep='')

当我这样做时它给了我

TypeError: open_new_tab() got an unexpected keyword argument 'sep'

但是如果我不能将“sep”放入该参数中,我还应该如何正确打开 link?

您可以使用

key_str = "".join(key)

key 中的所有字符连接成一个字符串。 URL 可以用

构造
url = f"https://bit.ly/{key_str}"

顺便说一下,您可以使用 string.ascii_letters.

而不是定义大小写字母列表