php Python的爱词霸(aiciba)词典
Python的爱词霸(aiciba)词典 [代码片段(47行)]
Python语言: Python的爱词霸(aiciba)词典 # -*- coding: utf-8 -*- # # Python的爱词霸(aiciba)词典 import urllib2 import sys import traceback import re from xml.etree.ElementTree import parse def lookup(word): dict_url="<a href="http://dict-co.iciba.com/api/dictionary.php?w=%s">http://dict-co.iciba.com/api/dictionary.php?w=%s" url = dict_url % word resXML = parse(urllib2.urlopen(url)).getroot() p = resXML.find("ps") pos = resXML.find("pos") d = resXML.find("acceptation") if p == None: print "没找到这词" else: print "音:" ,p.text.encode('utf8') print "词性:" ,pos.text.encode('utf8') print "含义:" ,d.text.encode('utf8') print len(resXML.findall("sent")) for i,sent in enumerate(resXML.findall("sent")): print i,sent[0].text print " " + sent[2].text.encode('utf8') if __name__ == "__main__": while(True): word = raw_input("\\n输入您要查询的单词(88是退出):") if word == "88": break elif re.search("^([a-zA-Z]*)$",word)==None: print "Does not support C to E" break else: try: lookup(word) except Exception,e: print traceback.format_exc() //该片段来自于http://outofmemory.cn
- 上一篇:php 字符串截取, 支持常用编码
- 下一篇:php 一个优雅的字节相关换算公式
精彩图集
精彩文章