单词词义、词性、例句查询python代码

该代码使用Python的requests和beautifulsoup4库实现从有道词典批量获取单词的发音、词义、词性和例句,并将结果保存到words.txt文件中。用户可在命令行输入多个单词,用斜杠分隔,程序会依次查询并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单词发音、词义、词性、例句查询、输出结果更简洁,一次可查多个单词

运行该代码,命令窗口输入单词,单词用“/”分开,例如:noisy/problem/community/neighbor 可以更多。先安装两个python包requests、 beautifulsoup4,前者爬取网页内容,后者分析提取需要的内容。拷贝这一行命令,在命令窗口(终端)里运行,需联网

pip install requests beautifulsoup4 -i http://pypi.douban.com/simple
import requests
from bs4 import BeautifulSoup
def query(word):
    # 获取有道词典网页的HTML代码
    url = 'http://dict.youdao.com/w/%s' % word
    html = requests.get(url).text

    # 使用BeautifulSoup解析HTML代码,并获取单词读音、词义和词性及例句
    soup = BeautifulSoup(html, 'html.parser')
    phonetic = soup.find(class_='phonetic').get_text().strip()   # 获取单词的读音
    trans_container = soup.find(id='phrsListTab')   # 获取词义和例句的容器
    trans = trans_container.find(class_='trans-container').get_text().strip() # 获取词义
    examples = soup.find_all(class_='examples')  # 获取例句

    # 输出结果
    with open('words.txt', 'a+', encoding="UTF-8") as f:
        f.write(word+"\n")
        f.write(phonetic+"\n")
        f.write(trans+"\n")
        for example in examples:
            f.write(example.get_text().strip()+"\n")

    print('%s %s' % (word, phonetic))
    print(trans)
    print("-" * 30)
    print("例句:")
    for example in examples:
        print(example.get_text().strip())

 #输入要查询的英语单词
Input_Word = input("请输入要查询的单词(单词用'/'分开):")
list_word = Input_Word.split('/')
for lst in list_word:
    query(lst)

查询结果:
noisy
[ˈnɔɪzi]
adj. 嗓门大的,聒噪的;嘈杂的,充满噪音的;吵吵嚷嚷的,哗众取宠的;(尤指电子设备的)干扰,干扰信息

[
比较级
noisier或more noisy
最高级
noisiest或most noisy
]
…my noisy old typewriter.
…我的噪音大的旧打字机。
The students on the grass bank cheered noisily.
学生们在草堤上喧闹地欢呼。
It’s a noisy place with film clips showing constantly on one of the cafe’s giant screens.
这是一个嘈杂的地方,电影片断不停地在咖啡厅其中一个大屏幕上放映。
The baggage hall was crowded and noisy.
行李大厅拥挤而嘈杂。
It might, at last, silence the small but noisy intellectual clique.
它或许最终能让那个人数不多却哗众取宠的知识分子群体安静下来。
problem
[ˈprɒbləm]
n. 问题,难题,困难;疾病;(通过算数或仔细思考而解决的)题;(感觉上)不喜欢,不情愿;(尤指国际象棋等)棋式,排局
adj. 难对付的,成问题的

[
复数
problems
]
…the economic problems of the inner city.
…内城的经济问题。
I do not have a simple solution to the drug problem.
我并没有一个简单的解决毒品问题的方法。
With mathematical problems, you can save time by approximating.
对于数学题,你可以用近似法省时间。
community
[kəˈmjuːnəti]
n. 社区,社会;(由同国籍、同宗教等构成的)群体,界;(多个国家的)共同体;归属感;(动植物的)群落

[
复数
communities
]
He’s well liked by people in the community.
他很受社区人们的喜爱。
The police haven’t really done anything for the black community in particular.
警方并没有特别为黑人团体做什么。
Two of our greatest strengths are diversity and community.
我们的两个最大优点是多样性和团体精神。
neighbor
[ˈneɪbə®]
n. 邻居
adj. 邻近的
vi. 友好;毗邻而居
vt. 邻接
n. (Neighbor)人名;(英)内伯

[
复数
neighbors
第三人称单数
neighbors
现在分词
neighboring
过去式
neighbored
过去分词
neighbored
]

win11 python3.9  Pycharm调试通过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值