今天看到园友 心白水关于必应字典API的应用 ,感觉很不错,所以用Python也写了一个。具体源码如下
1 import urllib.request
2 import json
3
4 serviceurl='http://xtk.azurewebsites.net/BingDictService.aspx?Word='
5
6 while True:
7 word = input('请输入英文单词: ')
8 if len(word) < 1 :
9 break
10 url = serviceurl + word
11 try:
12 uh = urllib.request.urlopen(url)
13 except:
14 print('API错误,请重试')
15 continue
16 data = uh.read()
17 try:
18 js = json.loads(data.decode('utf-8'))
19 except:
20 js = ''
21
22 if ( js["pronunciation"] == None):
23 print("未查到匹配单词")
24
25 else:
26 print("美音:[" ,js["pronunciation"]["AmE"], "] 英音:[",js["pronunciation"]["BrE"],"]\n")
27 for de in js["defs"]:
28 print(de["pos"],de["def"])
29 print("\n例句")
30 for sam in js["sams"]:
31 print("en:", sam["eng"])
32 print("中文:", sam["chn"],"\n")
33 continue
程序运行的结果如下:
请输入英文单词: python
美音:[ 'paɪθ��n ] 英音:[ 'paɪθ(ə)n ]
n. 蟒;蚺蛇
Web 蟒蛇;巨蟒;派森
例句
en: Serpent blood spurted through the air, and in a minute, the huge python was dead.
中文: 蛇血在空中喷射出来,很快,大蟒蛇就死了。
en: Suggests that Python Web services implementations were not the only ones having trouble with the air fare quote service.
中文: 指出,PythonWeb服务实现并不是惟一使用机票报价服务有困难的。
en: Another idea is to require python owners to have their pets fitted with microchips.
中文: 另一个想法是让养蛇人给他们的宠物配上微芯片。
en: A practice millennium python seeing, changes into an elegant young man, spits into the borneol in his mouth.
中文: 一修炼千年的巨蟒看见,化为一翩翩少年,将冰片吐入他口中。
en: Participants reacted to the meaning threat implicit in Python by handing out a larger notional punishment to a lawbreaker.
中文: 参与者的反应是在巨蟒中一个具有隐含意义的威胁派由一个的更大名义让处罚者进行处罚。
en: Quite a number of Python programmers have recommended Spark to me.
中文: 很多Python程序员都向我推荐Spark。
en: Zhu went out to investigate, and there, a few paces from his door, he saw a huge python!
中文: 祝出去查看,就在门口几步远的地方,他看到了一条巨蟒!
en: The balls disappeared, and the couple found a lumpy-looking carpet python nearby.
中文: 球不见了,夫妇却发现在附近有一条身形凹凸不平的地毯蟒。
en: So, in a second study they used a description of a Monty Python sketch which participants weren't told was supposed to be a joke.
中文: 因此,在第二个实验中,他们使用了一个描述巨蟒的素描,而参与者也没有认为这应该是一个玩笑。
en: The scripts directory contains python scripts used to install and uninstall the application.
中文: 脚本目录包含用于安装和卸载应用程序的python脚本。