#204 Dict.cn字典查询

#!/usr/bin/env python
#coding:utf-8
import urllib2,re

class Dict:
    """Class to get EN2CN & CN2EN translation from dict.cn"""

    def __init__( self, word="Della" ):
        """initializing the word and the url received"""   
        self.setWord(word)
        self.setUrl()


    def setWord( self, word ):
        """function to set the word"""
        self.__word=word


    def setUrl(self):
        """function to set the url"""
        self.__url="http://dict.cn/ws.php?utf8=true&q=%s"%self.__word


    def getPage(self):
        """function to get the content of the web page.return the string page content"""        
        url=self.__url
        try:
            page = urllib2.urlopen(url)
            page_content = page.read()
            page.close()
        except:
            return ""
        return page_content


    def getWord(self):
        """function get the info what we needed from the web page.return a list reply"""
        page_content=self.getPage().replace("\n"," ")
        page_content=unicode(page_content,"utf-8") # set the page content encoding to unicode
        regex=r'<def>(.*)</def>' 
        match=re.findall( regex , page_content )     
        return match