python英语字典查询单词
时间: 2023-06-24 12:05:41 浏览: 109
你可以使用Python的`requests`库和一个在线英语字典API进行单词查询。以下是一个简单的代码示例:
```python
import requests
def define_word(word):
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
for entry in data:
print(f"Definitions for {entry['word']}:")
for definition in entry['meanings'][0]['definitions']:
print(f"- {definition['definition']}")
else:
print("Word not found in dictionary.")
word = input("Enter a word to define: ")
define_word(word)
```
这个代码使用了"Dictionary API"这个在线英语字典API,你可以在这个网址找到它:https://dictionaryapi.dev/。
注意:在使用这个API时,你需要通过它的`/terms`端点接受服务条款,并且在每个请求中包含`User-Agent`头部,以标识你的应用程序。具体请查看API文档。