python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python 翻译

三大Python翻译神器再也不用担心学不好英语

作者:顾木子吖

本文介绍了用python做一款属于自己的翻译词典软件,从此告别网页搜索,文中有非常详细的代码示例,小伙伴们快快行动吧,需要的朋友可以参考下

导语

图片

hello everyone!  I'm kimiko!

​Miss me???

嘿!中秋结束了,开始正式营业给大家继续送福利啦!

图片

中秋在家发现:家里的小孩子在最后上学那天开始赶作业了!果然每个孩子的童年都是一样的!哈哈哈啊~

看了一眼侄女儿正在写英语翻译题目, 啊这一看不得了题目写的乱七八糟!!!

这让我有点儿上头啊,刚巧直接给我姐说了:‘'‘这英语还要加强学习撒'

老姐说:‘'回家我也不会教不了,学校的也没学好,一句完整的句子都讲不出来.......‘“

图片

那肯定是拿上我30米的鼠标键盘,开始敲代码!这款中英文翻译神器,小孩子学习英文很轻松啊~回家就给我侄女儿装上电脑了!

å¾ç

正文

你是不是也经常遇到这些问题:往下看!!今天教大家写一款三大翻译神器,你值得拥有!

环境安装:python3.6、pycharm2021,以及自带的模块。

pip install -i https://pypi.douban.com/simple/ requests 
pip install -i https://pypi.douban.com/simple/ pyqt5

​简单的界面可优化哈:

class Translator(QWidget):
	def __init__(self, parent=None, **kwargs):
		super(Translator, self).__init__(parent)
		self.setWindowTitle('三大翻译软件')
		self.setWindowIcon(QIcon('data/icon.jpg'))
		self.Label1 = QLabel('原文')
		self.Label2 = QLabel('译文')
		self.LineEdit1 = QLineEdit()
		self.LineEdit2 = QLineEdit()
		self.translateButton1 = QPushButton()
		self.translateButton2 = QPushButton()
		self.translateButton3 = QPushButton()
		self.translateButton1.setText('百度翻译')
		self.translateButton2.setText('有道翻译')
		self.translateButton3.setText('谷歌翻译')
		self.grid = QGridLayout()
		self.grid.setSpacing(12)
		self.grid.addWidget(self.Label1, 1, 0)
		self.grid.addWidget(self.LineEdit1, 1, 1)
		self.grid.addWidget(self.Label2, 2, 0)
		self.grid.addWidget(self.LineEdit2, 2, 1)
		self.grid.addWidget(self.translateButton1, 1, 2)
		self.grid.addWidget(self.translateButton2, 2, 2)
		self.grid.addWidget(self.translateButton3, 3, 2)
		self.setLayout(self.grid)
		self.resize(400, 150)
		self.translateButton1.clicked.connect(lambda : self.translate(api='baidu'))
		self.translateButton2.clicked.connect(lambda : self.translate(api='youdao'))
		self.translateButton3.clicked.connect(lambda : self.translate(api='google'))
		self.bd_translate = baidu()
		self.yd_translate = youdao()
		self.gg_translate = google()
	def translate(self, api='baidu'):
		word = self.LineEdit1.text()
		if not word:
			return
		if api == 'baidu':
			results = self.bd_translate.translate(word)
		elif api == 'youdao':
			results = self.yd_translate.translate(word)
		elif api == 'google':
			results = self.gg_translate.translate(word)
		else:
			raise RuntimeError('Api should be <baidu> or <youdao> or <google>...')
		self.LineEdit2.setText(';'.join(results))

三大翻译之一:百度翻译类。

class baidu():
	def __init__(self):
		self.session = requests.Session()
		self.session.cookies.set('BAIDUID', '19288887A223954909730262637D1DEB:FG=1;')
		self.session.cookies.set('PSTM', '%d;' % int(time.time()))
		self.headers = {
							'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
						}
		self.data = {
						'from': '',
						'to': '',
						'query': '',
						'transtype': 'translang',
						'simple_means_flag': '3',
						'sign': '',
						'token': '',
						'domain': 'common'
					}
		self.url = 'https://fanyi.baidu.com/v2transapi'
		self.langdetect_url = 'https://fanyi.baidu.com/langdetect'
	def translate(self, word):
		self.data['from'] = self.detectLanguage(word)
		self.data['to'] = 'en' if self.data['from'] == 'zh' else 'zh'
		self.data['query'] = word
		self.data['token'], gtk = self.getTokenGtk()
		self.data['token'] = '6482f137ca44f07742b2677f5ffd39e1'
		self.data['sign'] = self.getSign(gtk, word)
		res = self.session.post(self.url, data=self.data)
		return [res.json()['trans_result']['data'][0]['result'][0][1]]
	def getTokenGtk(self):
		url = 'https://fanyi.baidu.com/'
		res = requests.get(url, headers=self.headers)
		token = re.findall(r"token: '(.*?)'", res.text)[0]
		gtk = re.findall(r";window.gtk = ('.*?');", res.text)[0]
		return token, gtk
	def getSign(self, gtk, word):
		evaljs = js2py.EvalJs()
		js_code = js.bd_js_code
		js_code = js_code.replace('null !== i ? i : (i = window[l] || "") || ""', gtk)
		evaljs.execute(js_code)
		sign = evaljs.e(word)
		return sign
	def detectLanguage(self, word):
		data = {
				'query': word
				}
		res = self.session.post(self.langdetect_url, headers=self.headers, data=data)
		return res.json()['lan']

​三大翻译之二:有道翻译类。

class youdao():
	def __init__(self):
		self.headers = {
						'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
						'Referer': 'http://fanyi.youdao.com/',
						'Cookie': 'OUTFOX_SEARCH_USER_ID=-481680322@10.169.0.83;'
					}
		self.data = {
						'i': None,
						'from': 'AUTO',
						'to': 'AUTO',
						'smartresult': 'dict',
						'client': 'fanyideskweb',
						'salt': None,
						'sign': None,
						'ts': None,
						'bv': None,
						'doctype': 'json',
						'version': '2.1',
						'keyfrom': 'fanyi.web',
						'action': 'FY_BY_REALTlME'
					}
		self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
	def translate(self, word):
		ts = str(int(time.time()*10000))
		salt = ts + str(int(random.random()*10))
		sign = 'fanyideskweb' + word + salt + '97_3(jkMYg@T[KZQmqjTK'
		sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
		bv = '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
		bv = hashlib.md5(bv.encode('utf-8')).hexdigest()
		self.data['i'] = word
		self.data['salt'] = salt
		self.data['sign'] = sign
		self.data['ts'] = ts
		self.data['bv'] = bv
		res = requests.post(self.url, headers=self.headers, data=self.data)
		return [res.json()['translateResult'][0][0].get('tgt')]

​三大翻译之三:Google翻译类。

class google():
	def __init__(self):
		self.headers = {
						'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
					}
		self.url = 'https://translate.google.cn/translate_a/single?client=t&sl=auto&tl={}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&tk={}&q={}'
	def translate(self, word):
		if len(word) > 4891:
			raise RuntimeError('The length of word should be less than 4891...')
		languages = ['zh-CN', 'en']
		if not self.isChinese(word):
			target_language = languages[0]
		else:
			target_language = languages[1]
		res = requests.get(self.url.format(target_language, self.getTk(word), word), headers=self.headers)
		return [res.json()[0][0][0]]
	def getTk(self, word):
		evaljs = js2py.EvalJs()
		js_code = js.gg_js_code
		evaljs.execute(js_code)
		tk = evaljs.TL(word)
		return tk
	def isChinese(self, word):
		for w in word:
			if '\u4e00' <= w <= '\u9fa5':
				return True
		return False

效果如下:中英文各一句哈!

​总结

每天进步一点点!记得“三连哦~爱你,坚持学习!!!

到此这篇关于三大Python翻译神器再也不用担心学不好英语的文章就介绍到这了,更多相关Python 翻译内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文