google翻译自动检测语言翻译成中文
生活随笔
收集整理的這篇文章主要介紹了
google翻译自动检测语言翻译成中文
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import requests
import execjsclass Py4Js():def __init__(self):# 下面是某位大佬寫的獲取google翻譯變量的方法self.ctx = execjs.compile("""function TL(a) {var k = "";var b = 406644;var b1 = 3293161072;var jd = ".";var $b = "+-a^+6";var Zb = "+-3^+b+-f";for (var e = [], f = 0, g = 0; g < a.length; g++) {var m = a.charCodeAt(g);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),e[f++] = m >> 18 | 240,e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,e[f++] = m >> 6 & 63 | 128),e[f++] = m & 63 | 128)}a = b;for (f = 0; f < e.length; f++) a += e[f],a = RL(a, $b);a = RL(a, Zb);a ^= b1 || 0;0 > a && (a = (a & 2147483647) + 2147483648);a %= 1E6;return a.toString() + jd + (a ^ b)};function RL(a, b) {var t = "a";var Yb = "+";for (var c = 0; c < b.length - 2; c += 3) {var d = b.charAt(c + 2),d = d >= t ? d.charCodeAt(0) - 87 : Number(d),d = b.charAt(c + 1) == Yb ? a >>> d: a << d;a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d}return a}""")def getTk(self, text):#call()里面是要解析的函數(shù)名return self.ctx.call("TL", text)def translate( content):js = Py4Js()tk = js.getTk(content)if len(content) > 4891:print("翻譯的長度超過限制!!!")returnparam = {'tk': tk, 'q': content}result = requests.get("https://translate.google.cn/translate_a/single?client=webapp&sl=auto&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=0&tsel=0&kc=1", params=param)print("content:",content)print("-------------------")try:for text in result.json():return text[0][0]except:raiseif __name__ == "__main__":content = "Master"t=translate(content)print(t)
總結(jié)
以上是生活随笔為你收集整理的google翻译自动检测语言翻译成中文的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关系数据库理论:数据库的六大范式知识笔记
- 下一篇: 系统运维:收集6款经典的服务器监控工具