python越用越觉得顺手,简洁。不到50行代码就完成了,代码没有精简,要不然更短小~
查询是调用爱词霸的接口:http://dict-co.iciba.com/api/dictionary.php?w= 接口地址后面跟
查询的词汇
返回的是xml数值。里面附带所有信息,包括发音 的声音文件地址,如果想带发音的功能
可以直接使用它的文件,返回数值接口信息截图:
截图:
查询结果:
代码
www.zzzyk.com
打印帮助
1 #!/usr/bin/env python
2 #coding: utf-8
3 #Autor: return Blog:www.creturn.com Date:2012.11.11
4 import gtk,urllib
5 from xml.dom import minidom
6 def init():
7 global bufferText
8 global inputText
9 win = gtk.Window()
10 win.set_title('我的词典')
11 win.connect('destroy',lambda w:gtk.main_quit())
12 inputText = gtk.Entry()
13 findBtn = gtk.Button('查询')
14 viewText = gtk.TextView()
15 hbox = gtk.HBox()
16 vbox = gtk.VBox()
17 scroll = gtk.ScrolledWindow()
18 scroll.add(viewText)
19 bufferText = gtk.TextBuffer()
20 findBtn.connect('clicked',findWordEvent)
21 viewText.set_buffer(bufferText)
22 viewText.set_size_request(200,200)
23 hbox.set_size_request(200,30)
24 hbox.pack_start(inputText,False)
25 hbox.pack_start(findBtn,False)
26 vbox.pack_start(hbox)
27 vbox.pack_start(scroll)
28 win.add(vbox)
29 win.show_all()
30 gtk.main()
31 def findWordEvent(widget,data=None):
32 bufferText.set_text('正在查询...')
33 word = inputText.get_text()
34 xml = get_word_translate(word)
35 bufferText.set_text('')
36 doc = minidom.parseString(xml)
37 dicList = doc.getElementsByTagName('acceptation')
38 if len(dicList):
39 for dic in dicList:
40 bufferText.insert_at_cursor(dic.childNodes[0].nodeValue)
41 else:
42 bufferText.insert_at_cursor('查无此词')
43 def get_word_translate(word):
44 urlApi = 'http://dict-co.iciba.com/api/dictionary.php?w=%s'%word
45 result = urllib.urlopen(urlApi).read()
46 return result
47 def main():
48 init()
49 if __name__ == '__main__':
50 main()