当前位置:   article > 正文

python实现翻译功能_python 模拟有道翻译网页

python 模拟有道翻译网页

该程序为翻译软件,支持百度翻译、有道翻译、谷歌翻译三种翻译方式。用户输入需要翻译的文本,选择翻译方式(按钮),点击“翻译”按钮即可在下方显示翻译结果。程序使用Python编写,GUI使用PyQt5库,涉及到了requests、js2py等第三方库的使用,具有一定的技术难度。

 

  1. '''
  2. Function:
  3. 翻译软件V0.1.2,支持:
  4. --百度翻译
  5. --有道翻译
  6. --谷歌翻译
  7. '''
  8. import re
  9. import js
  10. import sys
  11. import time
  12. import js2py
  13. import random
  14. import hashlib
  15. import requests
  16. from PyQt5.QtGui import *
  17. from PyQt5.QtWidgets import *
  18. '''百度翻译类'''
  19. class baidu():
  20. def __init__(self):
  21. self.session = requests.Session()
  22. self.session.cookies.set('BAIDUID', '19288887A223954909730262637D1DEB:FG=1;')
  23. self.session.cookies.set('PSTM', '%d;' % int(time.time()))
  24. self.headers = {
  25. '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'
  26. }
  27. self.data = {
  28. 'from': '',
  29. 'to': '',
  30. 'query': '',
  31. 'transtype': 'translang',
  32. 'simple_means_flag': '3',
  33. 'sign': '',
  34. 'token': '',
  35. 'domain': 'common'
  36. }
  37. self.url = 'https://fanyi.baidu.com/v2transapi'
  38. self.langdetect_url = 'https://fanyi.baidu.com/langdetect'
  39. def translate(self, word):
  40. self.data['from'] = self.detectLanguage(word)
  41. self.data['to'] = 'en' if self.data['from'] == 'zh' else 'zh'
  42. self.data['query'] = word
  43. self.data['token'], gtk = self.getTokenGtk()
  44. self.data['token'] = '6482f137ca44f07742b2677f5ffd39e1'
  45. self.data['sign'] = self.getSign(gtk, word)
  46. res = self.session.post(self.url, data=self.data)
  47. return [res.json()['trans_result']['data'][0]['result'][0][1]]
  48. def getTokenGtk(self):
  49. url = 'https://fanyi.baidu.com/'
  50. res = requests.get(url, headers=self.headers)
  51. token = re.findall(r"token: '(.*?)'", res.text)[0]
  52. gtk = re.findall(r";window.gtk = ('.*?');", res.text)[0]
  53. return token, gtk
  54. def getSign(self, gtk, word):
  55. evaljs = js2py.EvalJs()
  56. js_code = js.bd_js_code
  57. js_code = js_code.replace('null !== i ? i : (i = window[l] || "") || ""', gtk)
  58. evaljs.execute(js_code)
  59. sign = evaljs.e(word)
  60. return sign
  61. def detectLanguage(self, word):
  62. data = {
  63. 'query': word
  64. }
  65. res = self.session.post(self.langdetect_url, headers=self.headers, data=data)
  66. return res.json()['lan']
  67. '''有道翻译类'''
  68. class youdao():
  69. def __init__(self):
  70. self.headers = {
  71. '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',
  72. 'Referer': 'http://fanyi.youdao.com/',
  73. 'Cookie': 'OUTFOX_SEARCH_USER_ID=-481680322@10.169.0.83;'
  74. }
  75. self.data = {
  76. 'i': None,
  77. 'from': 'AUTO',
  78. 'to': 'AUTO',
  79. 'smartresult': 'dict',
  80. 'client': 'fanyideskweb',
  81. 'salt': None,
  82. 'sign': None,
  83. 'ts': None,
  84. 'bv': None,
  85. 'doctype': 'json',
  86. 'version': '2.1',
  87. 'keyfrom': 'fanyi.web',
  88. 'action': 'FY_BY_REALTlME'
  89. }
  90. self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
  91. def translate(self, word):
  92. ts = str(int(time.time()*10000))
  93. salt = ts + str(int(random.random()*10))
  94. sign = 'fanyideskweb' + word + salt + '97_3(jkMYg@T[KZQmqjTK'
  95. sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
  96. bv = '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
  97. bv = hashlib.md5(bv.encode('utf-8')).hexdigest()
  98. self.data['i'] = word
  99. self.data['salt'] = salt
  100. self.data['sign'] = sign
  101. self.data['ts'] = ts
  102. self.data['bv'] = bv
  103. res = requests.post(self.url, headers=self.headers, data=self.data)
  104. return [res.json()['translateResult'][0][0].get('tgt')]
  105. '''Google翻译类'''
  106. class google():
  107. def __init__(self):
  108. self.headers = {
  109. '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',
  110. }
  111. 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={}'
  112. def translate(self, word):
  113. if len(word) > 4891:
  114. raise RuntimeError('The length of word should be less than 4891...')
  115. languages = ['zh-CN', 'en']
  116. if not self.isChinese(word):
  117. target_language = languages[0]
  118. else:
  119. target_language = languages[1]
  120. res = requests.get(self.url.format(target_language, self.getTk(word), word), headers=self.headers)
  121. return [res.json()[0][0][0]]
  122. def getTk(self, word):
  123. evaljs = js2py.EvalJs()
  124. js_code = js.gg_js_code
  125. evaljs.execute(js_code)
  126. tk = evaljs.TL(word)
  127. return tk
  128. def isChinese(self, word):
  129. for w in word:
  130. if '\u4e00' <= w <= '\u9fa5':
  131. return True
  132. return False
  133. '''简单的Demo'''
  134. class Translator(QWidget):
  135. def __init__(self, parent=None, **kwargs):
  136. super(Translator, self).__init__(parent)
  137. self.setWindowTitle('翻译软件')
  138. self.Label1 = QLabel('原文')
  139. self.Label2 = QLabel('译文')
  140. self.LineEdit1 = QLineEdit()
  141. self.LineEdit2 = QLineEdit()
  142. self.translateButton1 = QPushButton()
  143. self.translateButton2 = QPushButton()
  144. self.translateButton3 = QPushButton()
  145. self.translateButton1.setText('百度翻译')
  146. self.translateButton2.setText('有道翻译')
  147. self.translateButton3.setText('谷歌翻译')
  148. self.grid = QGridLayout()
  149. self.grid.setSpacing(12)
  150. self.grid.addWidget(self.Label1, 1, 0)
  151. self.grid.addWidget(self.LineEdit1, 1, 1)
  152. self.grid.addWidget(self.Label2, 2, 0)
  153. self.grid.addWidget(self.LineEdit2, 2, 1)
  154. self.grid.addWidget(self.translateButton1, 1, 2)
  155. self.grid.addWidget(self.translateButton2, 2, 2)
  156. self.grid.addWidget(self.translateButton3, 3, 2)
  157. self.setLayout(self.grid)
  158. self.resize(400, 150)
  159. self.translateButton1.clicked.connect(lambda : self.translate(api='baidu'))
  160. self.translateButton2.clicked.connect(lambda : self.translate(api='youdao'))
  161. self.translateButton3.clicked.connect(lambda : self.translate(api='google'))
  162. self.bd_translate = baidu()
  163. self.yd_translate = youdao()
  164. self.gg_translate = google()
  165. def translate(self, api='baidu'):
  166. word = self.LineEdit1.text()
  167. if not word:
  168. return
  169. if api == 'baidu':
  170. results = self.bd_translate.translate(word)
  171. elif api == 'youdao':
  172. results = self.yd_translate.translate(word)
  173. elif api == 'google':
  174. results = self.gg_translate.translate(word)
  175. else:
  176. raise RuntimeError('Api should be <baidu> or <youdao> or <google>...')
  177. self.LineEdit2.setText(';'.join(results))
  178. '''run'''
  179. if __name__ == '__main__':
  180. app = QApplication(sys.argv)
  181. demo = Translator()
  182. demo.show()
  183. sys.exit(app.exec_())

js.py

  1. bd_js_code = r'''
  2. function a(r) {
  3. if (Array.isArray(r)) {
  4. for (var o = 0, t = Array(r.length); o < r.length; o++)
  5. t[o] = r[o];
  6. return t
  7. }
  8. return Array.from(r)
  9. }
  10. function n(r, o) {
  11. for (var t = 0; t < o.length - 2; t += 3) {
  12. var a = o.charAt(t + 2);
  13. a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
  14. a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
  15. r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
  16. }
  17. return r
  18. }
  19. function e(r) {
  20. var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
  21. if (null === o) {
  22. var t = r.length;
  23. t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
  24. } else {
  25. for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
  26. "" !== e[C] && f.push.apply(f, a(e[C].split(""))),
  27. C !== h - 1 && f.push(o[C]);
  28. var g = f.length;
  29. g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
  30. }
  31. var u = void 0
  32. , l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
  33. u = null !== i ? i : (i = window[l] || "") || "";
  34. for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
  35. var A = r.charCodeAt(v);
  36. 128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
  37. S[c++] = A >> 18 | 240,
  38. S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
  39. S[c++] = A >> 6 & 63 | 128),
  40. S[c++] = 63 & A | 128)
  41. }
  42. for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
  43. p += S[b],
  44. p = n(p, F);
  45. return p = n(p, D),
  46. p ^= s,
  47. 0 > p && (p = (2147483647 & p) + 2147483648),
  48. p %= 1e6,
  49. p.toString() + "." + (p ^ m)
  50. }
  51. '''
  52. gg_js_code = '''
  53. function TL(a) {
  54. var k = "";
  55. var b = 406644;
  56. var b1 = 3293161072;
  57. var jd = ".";
  58. var $b = "+-a^+6";
  59. var Zb = "+-3^+b+-f";
  60. for (var e = [], f = 0, g = 0; g < a.length; g++) {
  61. var m = a.charCodeAt(g);
  62. 128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
  63. e[f++] = m >> 18 | 240,
  64. e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
  65. e[f++] = m >> 6 & 63 | 128),
  66. e[f++] = m & 63 | 128)
  67. }
  68. a = b;
  69. for (f = 0; f < e.length; f++) a += e[f],
  70. a = RL(a, $b);
  71. a = RL(a, Zb);
  72. a ^= b1 || 0;
  73. 0 > a && (a = (a & 2147483647) + 2147483648);
  74. a %= 1E6;
  75. return a.toString() + jd + (a ^ b)
  76. };
  77. function RL(a, b) {
  78. var t = "a";
  79. var Yb = "+";
  80. for (var c = 0; c < b.length - 2; c += 3) {
  81. var d = b.charAt(c + 2),
  82. d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
  83. d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
  84. a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
  85. }
  86. return a
  87. }
  88. '''
'
运行
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/855633
推荐阅读
相关标签
  

闽ICP备14008679号