写了个升级版的有道词典
花了两个小时写了个升级版的有道词典,代码写的很乱(不是一般的乱)。。。{:10_291:}支持整句话的中英互译,
查询英语单词时可以查找:释义,近义词,同根词,音标,双语例句,网络词组,词根
最重要的一点{:10_267:}
每次翻译过的词语或者句子程序会自动保存到本地(‘系统文件,勿删.sys’),第二次翻译是就可以实现离线翻译了{:10_275:}
又正是因为这个原因,系统文件,勿删.sys这个文件会越来越大...
好了,废话不多说,好用不好用大家说了算!
show me the code!{:10_288:}
**** Hidden Message ***** from urllib.request import *
from urllib.parse import *
import pickle as p
import json,re,os
class Translate:
def __init__(self,key_words):
#存放网址
self.key_words = key_words
self.url_1 = Request('http://dict.youdao.com/search?q=' + self.key_words + '&keyfrom=fanyi.smartResult')
self.url_1.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36')
self.url_2 = Request('http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=fanyi.logo')
self.url_2.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36')
self.url_list =
#为self.find_meaning 初始化
self.date = {"type":'AUTO',
'i':self.key_words,
'doctype':'json',
'xmlVersion':'1.8',
'keyfrom':'fanyi.web',
'ue':'UTF-8',
'action':'FY_BY_CLICKBUTTON',
'typoResult':'true'}
self.date = urlencode(self.date).encode('utf-8')#编码
#存放翻译结果
self.translate_result = {self.key_words:{'meaning':[],
'pronunciation':{'英':'','美':''},
'serious_meaning':[],
'internet_phrase':{},
'synonym':{},
'conjugate':{},
'example_sentence':{}}}
def _load(self):
self.file = open('系统文件,勿删!.sys','rb')
self.dict = p.load(self.file)
self.file.close()
return self.dict
def _save(self):
self.old = self._load()
self.translate_result.update(self.old)
self.file = open('系统文件,勿删!.sys','wb')
dict = p.dump(self.translate_result,self.file)
self.file.close()
#在'http://fanyi.youdao.com/查找释义
def _find_meaning(self):
self.cc = json.loads(urlopen(self.url_list,self.date).read().decode())
#查找翻译结果
try:
self.cc['smartResult']['entries']
except KeyError:
self.translate_result['meaning'] = self.cc['translateResult']['tgt']
else:
for i in self.cc['smartResult']['entries']:
self.translate_result['meaning'].append(i)
#查找发音
def _find_pronunciation(self):
self.cc = re.findall('<span class="pronounce">英(?:\n|.)+?</span>',self.html)
self.dd = re.findall('<span class="pronounce">美(?:\n|.)+?</span>',self.html)
#英式
self.a = str(self.cc).find('[')
self.b = str(self.cc).find(']')
self.translate_result['pronunciation']['英'] = str(self.cc)
#美式
self.a = str(self.dd).find('[')
self.b = str(self.dd).find(']')
self.translate_result['pronunciation']['美'] = str(self.dd)
#查找详细的释义
def _find_meaning_seriously(self):
self.cc = re.findall(r'<div class="trans-container">(?:.|\n)+?</div>',self.html)
self.dd = re.findall(r'<li>[^\\].+?</li>',str(self.cc))
for each in self.dd:
self.ee = []
each = each.replace('<li>','')
each = each.replace('</li>','')
self.ee.append(each)
self.translate_result['serious_meaning'] = self.ee
#查找网络短语
def _find_internet_phrase(self):
self.cc = re.findall(r'<p class="wordGroup collapse">(?:.|\n)+?</p>',self.html)
self.dd = re.findall(r'<span(?:.|\n)+?</p>',str(self.cc))
for i in self.dd:
self.a = i.find('wordgroup')
self.b = i.find('</a>',self.a)
self.c = i.find('</span>',self.b)
self.d = i.find('</p>',self.c)
self.words = i
#去除所有不好看的字符
self.words = self.words.replace(' ','')
self.words = self.words.replace('\\','')
self.words = self.words.replace('\n','')
self.translate_result['internet_phrase']] = self.words
#查找近义词
def _find_synonym(self):
self.cc = re.findall(r'<div id="synonyms".+>(?:.|\n)+?</div>',self.html)
self.dd = re.findall(r'<li>.+?</li>',str(self.cc))
self.ee = re.findall(r'syno">.+?</a>',str(self.cc))
self.a = []
for i in self.ee:
self.a.append(i)
self.translate_result['synonym']] = self.a
#查找同根词
def _find_conjugate(self):
if '同根词' in self.html:
#查找词根
self.a = self.html.find('词根')
self.a = self.html.find('relword',self.a)
self.b = self.html.find('</a>',self.a)
self.d = self.html
self.translate_result['conjugate']['词根'] = self.d
self.cc = re.findall(r'<p class="wordGroup">\n.+?<span class="contentTitle">\n.+?<a class="search-js" href="/w/eng/.+/#keyfrom=dict.basic.relword">.+?</a>\n.+?</span>\n.+?\n.+</p>',self.html)
for i in self.cc:
self.a = i.find('relword')
self.b = i.find('</a>',self.a)
self.c = i.find('</a>')
self.d = i.find('</p>',self.c)
self.words = i
#去除所有不好看的字符
self.words = self.words.replace(' ','')
self.words = self.words.replace('\\','')
self.words = self.words.replace('\n','')
self.words = self.words.replace('</span>','')
self.translate_result['conjugate']] = self.words
#查找双语例句
def _find_example_sentence(self):
#英文
self.cc = re.findall(r'<span id="src_._." onmouseover="hlgt\(\'#src_._.,#tran_._.\'\)" onmouseout="unhlgt\(\'#src_._.,#tran_._.\'\)">.+</span>',self.html)
#释义
self.dd = re.findall(r'<span id="tran.+.+</span>',self.html)
self.ff = []
self.ee = []
for each in range(3):
self.ff.append(re.findall(r'>.+?</span>',self.cc))
self.ee.append(re.findall(r'>.+?</span>',self.dd))
for i in self.ff:
if i == ' ':
self.ff.remove(i)
for i in self.ee:
if i == ' ':
self.ee.remove(i)
#拼接为一句话
for each in range(3):
self.e = ''
self.c = ''
for i in self.ee:
self.e += i
for b in self.ff:
self.c += b
#去除不好看的字符
self.e = self.e.replace('</span>','')
self.e = self.e.replace('>','')
self.e = self.e.replace('<','')
self.e = self.e.replace('b','')
self.e = self.e.replace('/','')
self.c = self.c.replace('</span>','')
self.c = self.c.replace('>','')
self.c = self.c.replace('<','')
self.c = self.c.replace('b','')
self.c = self.c.replace('/','')
#添加换行符
self.e += '\n'
self.c += '\n'
self.translate_result['example_sentence'] = self.c
def _output(self):
#打印'meaning'
if self.translate_result['meaning'] != []:
print('这个词的意思是: \n')
for i in self.translate_result['meaning']:
if not isinstance(i,str):
print(i)
else:
print(self.translate_result['meaning'])
print('\n')
break
#打印发音
if self.translate_result['pronunciation']['英'] != '':
print('发音:\n')
print('英式: %s' % self.translate_result['pronunciation']['英'])
print('美式: %s' % self.translate_result['pronunciation']['美'])
print('\n')
#打印'serious_meaning'
if self.translate_result['serious_meaning'] != []:
print('说专业一点,这个词的意思是:\n')
for i in self.translate_result['serious_meaning']:
print(i)
print(' ')
#打印网络词组
if self.translate_result['internet_phrase'] != {}:
print('相关的网络词组: \n')
for each in self.translate_result['internet_phrase'].items():
print('%s ------ %s' % (each,each))
print('\n')
#打印近义词
if self.translate_result['synonym'] != {}:
print('这个词语的近义词是:\n')
for each in self.translate_result['synonym'].items():
print(each)
for i in each:
print(i)
print('\n')
#打印同根词
if self.translate_result['conjugate'] != {}:
for each in self.translate_result['conjugate'].items():
print('%s ------ %s' % (each,each))
print('\n')
#打印双语例句
if self.translate_result['example_sentence'] != {}:
print('例如:\n')
for each in self.translate_result['example_sentence'].items():
print('%s ------ %s' % (each,each))
print('\n')
print('------------------------------华丽的分割线-------------------------------')
print('\n\n')
#程序从这里开始运行
def start(self):
self.old_dict = self._load()
if self.key_words not in self.old_dict:
try:
#'http://dict.youdao.com/search?q=' + self.key_words + '&keyfrom=fanyi.smartResult' 返回值
self.html = self.html = urlopen(self.url_list).read().decode()
#在'http://fanyi.youdao.com/查找释义
self._find_meaning()
#查找发音
self._find_pronunciation()
#查找详细的释义
self._find_meaning_seriously()
#查找网络短语
self._find_internet_phrase()
#查找近义词
self._find_synonym()
#查找同根词
self._find_conjugate()
#查找双语例句
self._find_example_sentence()
#保存
self._save()
#打印
self._output()
except:
self._find_meaning()
self._save()
self._output()
else:
self.translate_result = self.old_dict
self._output()
def look_all():
file = open('系统文件,勿删!.sys','rb')
dict = p.load(file)
file.close()
for each in dict.keys():
print(each)
def go(words):
url = 'http://dict.youdao.com/search?q=' + words + '&keyfrom=fanyi.smartResult'
os.system('start %s' % url)
def restart():
while True:
key_words = input('请输入需要翻译的内容:【输入0退出:】 ')
if key_words == '':
print('你什么也不输入我也退出>_<')
break
if key_words != '0':
cc = Translate(key_words)
cc.start()
else:
print('谢谢使用!')
print('程序退出...')
break
if __name__ == '__main__':
restart() 原来是老司机,失敬失敬 —— 代码写得很漂亮,足以见得功力深厚!{:10_256:} 大佬们以为如何?{:10_307:}@冬雪雪冬 @wei_Y @SixPy @hldh214 @shuofxz @新手·ing 用什么写的~ 一定要看的{:10_254:} ~风介~ 发表于 2017-4-9 00:01
{:10_297:}向大佬学习~ self.e = self.e.replace('</span>','')
self.e = self.e.replace('>','')
self.e = self.e.replace('<','')
self.e = self.e.replace('b','')
self.e = self.e.replace('/','')
self.c = self.c.replace('</span>','')
self.c = self.c.replace('>','')
self.c = self.c.replace('<','')
self.c = self.c.replace('b','')
self.c = self.c.replace('/','')
写成循环好点吧,可以重用。
@~风介~ 调皮的介哥~。 {:7_129:} 学习学习 ~风介~ 发表于 2017-4-9 00:02
原来是老司机,失敬失敬 —— 代码写得很漂亮,足以见得功力深厚!
哪里哪里{:10_256:} SixPy 发表于 2017-4-9 01:00
用什么写的~
python 看看怎么写的,学习下,支持
厉害
MSK好厉害。 加个gui是更好滴 看看大神们的作品 因为在学校准备中考来不及一一回复,抱歉 看看 完美运行,不错,向上面说的加个GUI更好了