Python3調國內的谷歌翻譯
阿新 • • 發佈:2018-12-05
公司最近有翻譯的需求,發現Google的Translate API只能FQ調,多方查詢彙集眾家之長,找到一個感覺最好用的,稍稍改進了一下,原址:
https://blog.csdn.net/andeyeluguo/article/details/78581590
很多部落格有提到tk值要js處理,不多說,直接上乾貨
首先,是要pip處理js的模組
pip install PyExecJS
建立HandleJs.py
1 import execjs 2 3 class Py4Js(): 4 5 def __init__(self):6 self.ctx = execjs.compile(""" 7 function TL(a) { 8 var k = ""; 9 var b = 406644; 10 var b1 = 3293161072; 11 12 var jd = "."; 13 var $b = "+-a^+6"; 14 var Zb = "+-3^+b+-f"; 15 16 for (var e = [], f = 0, g = 0; g < a.length; g++) { 17 var m = a.charCodeAt(g); 18 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), 19 e[f++] = m >> 18 | 240, 20 e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, 21 e[f++] = m >> 6 & 63 | 128), 22 e[f++] = m & 63 | 128) 23 } 24 a = b; 25 for (f = 0; f < e.length; f++) a += e[f], 26 a = RL(a, $b); 27 a = RL(a, Zb); 28 a ^= b1 || 0; 29 0 > a && (a = (a & 2147483647) + 2147483648); 30 a %= 1E6; 31 return a.toString() + jd + (a ^ b) 32 }; 33 34 function RL(a, b) { 35 var t = "a"; 36 var Yb = "+"; 37 for (var c = 0; c < b.length - 2; c += 3) { 38 var d = b.charAt(c + 2), 39 d = d >= t ? d.charCodeAt(0) - 87 : Number(d), 40 d = b.charAt(c + 1) == Yb ? a >>> d: a << d; 41 a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d 42 } 43 return a 44 } 45 """) 46 47 def getTk(self,text): 48 return self.ctx.call("TL",text)
建立main.py
1 import urllib.request 2 import urllib.parse 3 from HandleJs import Py4Js 4 5 6 def open_url(url): 7 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} 8 req = urllib.request.Request(url=url, headers=headers) 9 response = urllib.request.urlopen(req) 10 data = response.read().decode('utf-8') 11 return data 12 13 14 def buildUrl(content, tk, tl): 15 baseUrl = 'http://translate.google.cn/translate_a/single' 16 baseUrl += '?client=t&' 17 baseUrl += 'sl=auto&' 18 baseUrl += 'tl=' + str(tl) + '&' 19 baseUrl += 'hl=zh-CN&' 20 baseUrl += 'dt=at&' 21 baseUrl += 'dt=bd&' 22 baseUrl += 'dt=ex&' 23 baseUrl += 'dt=ld&' 24 baseUrl += 'dt=md&' 25 baseUrl += 'dt=qca&' 26 baseUrl += 'dt=rw&' 27 baseUrl += 'dt=rm&' 28 baseUrl += 'dt=ss&' 29 baseUrl += 'dt=t&' 30 baseUrl += 'ie=UTF-8&' 31 baseUrl += 'oe=UTF-8&' 32 baseUrl += 'clearbtn=1&' 33 baseUrl += 'otf=1&' 34 baseUrl += 'pc=1&' 35 baseUrl += 'srcrom=0&' 36 baseUrl += 'ssel=0&' 37 baseUrl += 'tsel=0&' 38 baseUrl += 'kc=2&' 39 baseUrl += 'tk=' + str(tk) + '&' 40 baseUrl += 'q=' + content 41 return baseUrl 42 43 44 def translate(content, tk, tl): 45 if len(content) > 4891: 46 print("翻譯的長度超過限制!!!") 47 return 48 49 content = urllib.parse.quote(content) 50 url = buildUrl(content, tk, tl) 51 52 # 返回值是一個多層巢狀列表的字串形式,解析起來還相當費勁,寫了幾個正則,發現也很不理想, 53 # 後來感覺,使用正則簡直就是把簡單的事情複雜化,這裡直接切片就Ok了 54 result = open_url(url) 55 56 end = result.find("\",") 57 if end > 4: 58 print(result[4:end]) 59 60 61 def main(): 62 js = Py4Js() 63 #content是要翻譯的內容 64 #tl是要翻譯的目標語種,值參照ISO 639-1標準,如果翻譯成中文"zh/zh-CN簡體中文" 65 content = "hello" 66 tl = "zh" 67 tk = js.getTk(content) 68 translate(content, tk, tl) 69 70 71 if __name__ == "__main__": 72 main()