你好,我想编写一个python程序,打开一个网站。当您输入快捷方式时,例如“谷歌”它将打开“https://www.google.de/”。问题是它不会打开正确的URL。
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)
使用您的示例,您可以:
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])
怎么样:
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
一样跑