`
wanglei6744
  • 浏览: 25594 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ubuntu 下 在线词典 (python 实现)

阅读更多



     毕设搞完了,又把电脑的系统换回了ubuntu,发现了ubuntu 下没有一个好用的词典,这对于我这个英语四级考了三次才过的人可谓是非常的不爽,看到网上有这么多的在线词典api,于是就用python 写了一个, 感觉还不错啊 ,在这贴上代码:

#!/usr/bin/env python



from xml.etree.ElementTree import XMLParser

from xml.parsers.expat import ExpatError

import json

import re

import urllib



class resultParser():

    

    def __init__(self):

        self.flag = True

    

    def start(self,tag,attrib):

        pass

     

    def data(self,data):

        if  data.find('.mp3') == -1 and data != word:

            print data

            self.flag = False

        

    def end(self,tag):

        pass

    

if __name__ == '__main__':

    while(True):

        word = raw_input("word:")

        if word == 'exit':

            break

        url = r'http://dict-co.iciba.com/api/dictionary.php?w='

        result = urllib.urlopen(url+word).read()

        target = resultParser()

        parser = XMLParser(target = target)

        try:

            parser.feed(result)

        except ExpatError:

            pass

        if target.flag:

           print 'no word : '+word +'\nplease check it again!'

 

 
  • 大小: 112.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics