有没有一种方法可以在输入后使用Python打开特定的网页?

有没有一种方法可以在输入后使用Python打开特定的网页?,python,python-webbrowser,Python,Python Webbrowser,我试图找到一种方法来编写一个脚本,接受用户的输入,然后打开网页。到目前为止,代码如下所示: jurisdiction = input("Enter jurisdiction:") if jurisdiction = 'UK': import webbrowser webbrowser.open('https://www.legislation.gov.uk/new') webbrowser.open('https://eur-lex.europa.eu

我试图找到一种方法来编写一个脚本,接受用户的输入,然后打开网页。到目前为止,代码如下所示:

jurisdiction = input("Enter jurisdiction:")
if jurisdiction = 'UK':
    import webbrowser
    webbrowser.open('https://www.legislation.gov.uk/new')
    webbrowser.open('https://eur-lex.europa.eu/oj/direct-access.html')
elif jurisdiction = Australia:
    import webbrowswer
    webbrowser.open('https://www.legislation.gov.au/WhatsNew')
else:
    print("Re-enter jurisdiction")
这导致第3行出现语法错误:

File "UK.py", line 3
if jurisdiction = UK
                ^
SyntaxError: invalid syntax**

我想知道代码中是否有我遗漏或不应该遗漏的内容?另外,有没有其他方法可以实现我在这里尝试实现的目标?

我建议大家阅读Python字符串比较。很容易修复,但您将受益于对字符串比较在Python中如何工作和如何不工作的基本理解

英国和澳大利亚也需要成为字符串

并且不要在代码体中导入webbroswer包。你只需要做一次

import webbrowser

jurisdiction = input("Enter jurisdiction:")
if jurisdiction == 'UK':
    webbrowser.open('https://www.legislation.gov.uk/new')
    webbrowser.open('https://eur-lex.europa.eu/oj/direct-access.html')
elif jurisdiction == 'Australia':
    webbrowser.open('https://www.legislation.gov.au/WhatsNew')
else:
    print("Re-enter jurisdiction")
更清洁的方法:

import webbrowser

mapping = {'UK': ['https://www.legislation.gov.uk/new', 'https://eur-lex.europa.eu/oj/direct-access.html'],
           'Australia': ['https://www.legislation.gov.au/WhatsNew']}

jurisdiction = input("Enter jurisdiction:")
urls = mapping.get(jurisdiction)
if urls is not None:
    for url in urls:
        webbrowser.open(url)
else:
    print("Re-enter jurisdiction")
使用==代替=