#-*- coding:utf-8 -*- from ..base import WebService, export, register, with_styles, parse_html @register(u'海词迷你词典') class MiniDict(WebService): def __init__(self): super(MiniDict, self).__init__() def _get_content(self): data = self.get_response(u"http://apii.dict.cn/mini.php?q={}".format(self.word)) soup = parse_html(data) result = { 'expressions': u'', 'sentences': u'', 'variations': u'', 'phonetic': u'', } # 音标 tag = soup.find('span', class_='p') if tag: result['phonetic'] = str(tag.get_text()).encode('utf-8') tag.decompose() # 基本释义 tag = soup.find('div', id='e') if tag: result['expressions'] = str(tag).encode('utf-8') tag.decompose() # 例句与用法 tag = soup.find('div', id='s') if tag: result['sentences'] = str(tag).encode('utf-8') tag.decompose() # 词形变化 tag = soup.find('div', id='t') if tag: result['variations'] = str(tag).encode('utf-8') tag.decompose() return self.cache_this(result) def _get_field(self, key, default=u''): return self.cache_result(key) if self.cached(key) else self._get_content().get(key, default) @export(u'音标') def fld_phonetic(self): return self._get_field('phonetic') @export(u'基本释义') def fld_explains(self): return self._get_field('expressions') @export(u'例句与用法') @with_styles(css='em {color:#cc0066;font-style:normal;}', need_wrap_css=True, wrap_class='minidict') def fld_sentences(self): return self._get_field('sentences') @export(u'词形变化') def fld_variations(self): return self._get_field('variations')