Python: python写的一个简单网络词典

闲来无事, 玩玩python...

是采用有道翻译, 然后抓取网页的.

 

import re, urllib url="http://dict.youdao.com/search?le=eng&q=" print ("input q to exit") while 1: word = raw_input(">>>") if word=="q": exit() else: word = word.replace(' ', '+') url += word url += "&tab=chn&keyfrom=dict.top" s = urllib.urlopen(url).read() comm1 = re.compile(';</td><td class="attributem1web">.*?</td>') result=comm1.findall(s) comm2 = re.compile(r'<td class="dttitle2"><font color="#013694"><b>.*?<//b><//font><//td>') related = comm2.findall(s) #find the result if result: print 'meaning:' for i in result: temp = i.decode('utf8').encode('cp936') temp = temp[33:] temp = temp[:-5] print temp print '/n' #fine the matters if related: print 'related:' for i in related: temp = i.decode('utf8').encode('cp936') temp = temp[46:] temp = temp[:-16] print temp print '/n' else: print 'no such word!'

你可能感兴趣的:(Web,网络,python,url,Class,input)