Python代码-查询Baidu词典
Baidu词典(http://dict.baidu.com/)是我比较喜欢的一个网络词典,但不喜欢每次都用Web去查,写了下面一段代码可以实现Baidu词典的脚本查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #!/usr/bin/env python import os,sys import urllib2 import re class BaiduDict: def __init__(self): pass def query(self, word): url = "http://www.baidu.com/baidu?ie=utf-8&ct=1048576&word=" try: data = urllib2.urlopen(url+urllib2.quote(word)).read() except: print "ERROR: can not connect to http://www.baidu.com/" return None p = re.compile("<ol>.+$", re.M) data = data.decode('gb2312').encode('UTF-8') match = p.search(data) if match: result = data[match.start():match.end()] result = re.sub("</div>","\n",result) result = re.sub("<[^>]+>", " ",result) else: result = "" return result if __name__ == '__main__': if len(sys.argv) > 1 : qword = sys.argv[1] print "================BaiduDict========================" baidu=BaiduDict() print baidu.query(qword) |
如果要查多于一个单词的短语的话,需要在短语外加引号就可以了。