|
发表于 2017-4-9 00:01:39
|
显示全部楼层
- 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.url_2,self.url_1]
- #为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[0],self.date).read().decode())
- #查找翻译结果
- try:
- self.cc['smartResult']['entries'][1:]
- except KeyError:
- self.translate_result[self.key_words]['meaning'] = self.cc['translateResult'][0][0]['tgt']
- else:
- for i in self.cc['smartResult']['entries'][1:]:
- self.translate_result[self.key_words]['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[0]).find('[')
- self.b = str(self.cc[0]).find(']')
- self.translate_result[self.key_words]['pronunciation']['英'] = str(self.cc[0])[self.a:self.b+1]
- #美式
- self.a = str(self.dd[0]).find('[')
- self.b = str(self.dd[0]).find(']')
- self.translate_result[self.key_words]['pronunciation']['美'] = str(self.dd[0])[self.a:self.b+1]
-
- #查找详细的释义
- 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[self.key_words]['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.c+7:self.d]
- #去除所有不好看的字符
- self.words = self.words.replace(' ','')
- self.words = self.words.replace('\\','')
- self.words = self.words.replace('\n','')
- self.translate_result[self.key_words]['internet_phrase'][i[self.a+11:self.b]] = 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[6:-4])
- self.translate_result[self.key_words]['synonym'][self.dd[0][4:-5]] = 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.a+9:self.b]
- self.translate_result[self.key_words]['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[self.a:])
- 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.c+4:self.d]
- #去除所有不好看的字符
- 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[self.key_words]['conjugate'][i[self.a+9:self.b]] = 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[each]))
- self.ee.append(re.findall(r'>.+?</span>',self.dd[each]))
- for i in self.ff:
- if i[2] == ' ':
- self.ff.remove(i)
- for i in self.ee:
- if i[2] == ' ':
- self.ee.remove(i)
-
- #拼接为一句话
- for each in range(3):
- self.e = ''
- self.c = ''
- for i in self.ee[each]:
- self.e += i
- for b in self.ff[each]:
- 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[self.key_words]['example_sentence'][self.e] = self.c
-
-
- def _output(self):
- #打印'meaning'
- if self.translate_result[self.key_words]['meaning'] != []:
- print('这个词的意思是: \n')
- for i in self.translate_result[self.key_words]['meaning']:
- if not isinstance(i,str):
- print(i)
- else:
- print(self.translate_result[self.key_words]['meaning'])
- print('\n')
- break
-
- #打印发音
- if self.translate_result[self.key_words]['pronunciation']['英'] != '':
- print('发音:\n')
- print('英式: %s' % self.translate_result[self.key_words]['pronunciation']['英'])
- print('美式: %s' % self.translate_result[self.key_words]['pronunciation']['美'])
- print('\n')
-
- #打印'serious_meaning'
- if self.translate_result[self.key_words]['serious_meaning'] != []:
- print('说专业一点,这个词的意思是:\n')
- for i in self.translate_result[self.key_words]['serious_meaning']:
- print(i)
- print(' ')
-
- #打印网络词组
- if self.translate_result[self.key_words]['internet_phrase'] != {}:
- print('相关的网络词组: \n')
- for each in self.translate_result[self.key_words]['internet_phrase'].items():
- print('%s ------ %s' % (each[0],each[1]))
- print('\n')
-
- #打印近义词
- if self.translate_result[self.key_words]['synonym'] != {}:
- print('这个词语的近义词是:\n')
- for each in self.translate_result[self.key_words]['synonym'].items():
- print(each[0])
- for i in each[1]:
- print(i)
- print('\n')
-
- #打印同根词
- if self.translate_result[self.key_words]['conjugate'] != {}:
- for each in self.translate_result[self.key_words]['conjugate'].items():
- print('%s ------ %s' % (each[0],each[1]))
- print('\n')
-
- #打印双语例句
- if self.translate_result[self.key_words]['example_sentence'] != {}:
- print('例如:\n')
- for each in self.translate_result[self.key_words]['example_sentence'].items():
- print('%s ------ %s' % (each[0],each[1]))
- 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[1]).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()
复制代码 |
|