---
title: "ubuntu 下 在线词典 （python 实现）"
author: "wanglei6744"
date: "2011-04-15T20:29:57Z"
tags: ["PHP", "Python", "Ubuntu", "XML", "json"]
categories: ["编程语言"]
slug: "ubuntu-python"
draft: false
lastmod: "2011-04-15T21:48:39Z"
id: 1004595
url: "https://lei6744.iteye.com/blog/1004595"
---

毕设搞完了，又把电脑的系统换回了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!'
```

   
![](http://dl.iteye.com/upload/attachment/464533/29dad532-4b34-3fcb-b2b3-d0194b510bd9.jpg)