如何用url替换输入

问题描述 投票:0回答:2

你好,我想编写一个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)
python
2个回答
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])

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一样跑

© www.soinside.com 2019 - 2024. All rights reserved.