python英语字典查询单词
时间: 2023-11-03 18:53:38 浏览: 93
可以使用Python的`requests`和`BeautifulSoup`库来实现英语字典查询单词。以下是一个例子:
```python
import requests
from bs4 import BeautifulSoup
# 输入要查询的单词
word = input("Enter a word to look up: ")
# 发送GET请求到字典网站
response = requests.get("https://www.dictionary.com/browse/" + word)
# 解析HTML页面
soup = BeautifulSoup(response.content, 'html.parser')
# 提取单词的定义
definitions = soup.find_all('div', {'value': '1'})
if len(definitions) > 0:
print("Definitions of " + word + ":")
for i, definition in enumerate(definitions):
print(str(i+1) + ". " + definition.text)
else:
print("Sorry, " + word + " was not found in the dictionary.")
```
这段代码将从用户输入中获取要查询的单词,然后使用`requests`库向`https://www.dictionary.com`发送GET请求,并使用`BeautifulSoup`库解析HTML页面。如果查询到了单词,它将打印出每个定义。如果没有找到该单词,则会打印出相应的消息。