您好,我想编写一个python程序,以打开一个网站。当您只输入快捷方式时,例如“ google”,它将打开“ https://www.google.de/”。问题在于它无法打开正确的网址。
import webbrowser
# URL list
google = "https://www.google.de"
ebay = "https://www.ebay.de/"
# shortcuts
Websites = ("google", "ebay")
def inputString():
inputstr = input()
if inputString(google) = ("https://www.google.de")
else:
print("Please look for the right shortcut.")
return
url = inputString()
webbrowser.open(url)
答案 0 :(得分:3)
使用您的示例,您可以执行以下操作:
google = "https://www.google.de"
ebay = "https://www.ebay.de/"
def inputString():
return input()
if inputString() == "google":
url = google
webbrowser.open(url)
或者您也可以按照@torxed所说的简单方法进行操作:
inputstr = input()
sites = {'google' : 'https://google.de', 'ebay':'https://www.ebay.de/'}
if inputstr in sites:
webbrowser.open(sites[inputstr])
答案 1 :(得分:0)
怎么样:
import webbrowser
import sys
websites = {
"google":"https://www.google.com",
"ebay": "https://www.ebay.com"
}
if __name__ == "__main__":
try:
webbrowser.open(websites[sys.argv[1]])
except:
print("Please look for the right shortcut:")
for website in websites:
print(website)
像这样python browse.py google