Python3 动手自己写谷歌翻译
本篇为实现谷歌翻译的功能,在编写的时候以为只是一个接口的问题。
没想到的是每次翻译都会触发一次JS的执行,在请求参数中生成一个tk。
文中tk的实现是复用的网上大神的代码生成tk。
好了,不说了直接看代码吧!
1 # /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import urllib.parse 4 import execjs,requests,termcolor 5 6 class Return_tk(): 7 8 def __init__(self): 9 self.ctx = execjs.compile(""" 10 function TL(a) { 11 var k = ""; 12 var b = 406644; 13 var b1 = 3293161072; 14 15 var jd = "."; 16 var $b = "+-a^+6"; 17 var Zb = "+-3^+b+-f"; 18 19 for (var e = [], f = 0, g = 0; g < a.length; g++) { 20 var m = a.charCodeAt(g); 21 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), 22 e[f++] = m >> 18 | 240, 23 e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, 24 e[f++] = m >> 6 & 63 | 128), 25 e[f++] = m & 63 | 128) 26 } 27 a = b; 28 for (f = 0; f < e.length; f++) a += e[f], 29 a = RL(a, $b); 30 a = RL(a, Zb); 31 a ^= b1 || 0; 32 0 > a && (a = (a & 2147483647) + 2147483648); 33 a %= 1E6; 34 return a.toString() + jd + (a ^ b) 35 }; 36 37 function RL(a, b) { 38 var t = "a"; 39 var Yb = "+"; 40 for (var c = 0; c < b.length - 2; c += 3) { 41 var d = b.charAt(c + 2), 42 d = d >= t ? d.charCodeAt(0) - 87 : Number(d), 43 d = b.charAt(c + 1) == Yb ? a >>> d: a << d; 44 a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d 45 } 46 return a 47 } 48 """) 49 50 def getTk(self, text): 51 return self.ctx.call("TL", text) 52 53 def open_url(url): 54 headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'} 55 req = requests.get(url=url, headers=headers) 56 return req.content.decode('utf-8') 57 58 def max_length(content): 59 if len(content) > 4891: 60 print("翻译文本超过限制!") 61 return 62 63 def print_result(parm): 64 result = parm 65 str_end = result.find("\",") 66 if str_end > 4: 67 print("翻译的结果为:",result[4:str_end]) 68 69 def en_to_zn_translate(content, tk): 70 max_length(content) 71 content = urllib.parse.quote(content) 72 #英译汉 73 url = "http://translate.google.cn/translate_a/single?client=t" \ 74 "&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" \ 75 "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" \ 76 "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (tk, content) 77 result = open_url(url) 78 print_result(result) 79 80 def zn_to_en_translate(content,tk): 81 max_length(content) 82 content = urllib.parse.quote(content) 83 #汉译英 84 url = "http://translate.google.cn/translate_a/single?client=t"\ 85 "&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca"\ 86 "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8"\ 87 "&source=btn&ssel=3&tsel=3&kc=0&tk=%s&q=%s"%(tk,content) 88 result = open_url(url) 89 print_result(result) 90 91 def main(): 92 js = Return_tk() 93 while True: 94 print(termcolor.colored("请先输入要进行的操作:q表示退出;e表示英文翻译成中文;z表示中文翻译成英文。","red")) 95 change = input("请选择翻译选项:") 96 if change == 'q': 97 break 98 elif change == 'e': 99 content = input("请输入要翻译的英文:") 100 tk = js.getTk(content) 101 en_to_zn_translate(content,tk) 102 elif change == 'z': 103 content = input("请输入要翻译的中文:") 104 tk = js.getTk(content) 105 zn_to_en_translate(content,tk) 106 else: 107 print("请输入正确的选项!") 108 109 main()
见运行效果:
总结:
从代码上来看,写的比较简洁,没有高大上的写法,只是单纯的实现一个功能。
除了tk的生成封装成了类,其他的全部是用函数式编程实现。