有没有办法在输入后使用 Python 打开特定网页?
Is there a way to use Python to open specific webpages after typing in input?
我正在尝试找到一种方法来编写接受用户输入的脚本,之后它将打开网页。到目前为止,代码如下所示:
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")
我正在尝试找到一种方法来编写接受用户输入的脚本,之后它将打开网页。到目前为止,代码如下所示:
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")