输入输入后,是否可以使用Python打开特定网页?

问题描述

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

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中起作用和不起作用具有基本了解。

英国和澳大利亚也都必须是Strings ...

并且不要在代码主体中导入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")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...