本篇爲實現谷歌翻譯的功能,在編寫的時候覺得只是一個接口的問題。python
沒想到的是每次翻譯都會觸發一次JS的執行,在請求參數中生成一個tk。編程
文中tk的實現是複用的網上大神的代碼生成tk。ide
好了,不說了直接看代碼吧!函數式編程
# /usr/bin/env python# -*- coding: utf-8 -*-import urllib.parseimport execjs,requests,termcolorclass Return_tk(): def __init__(self): 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): return self.ctx.call("TL", text)def open_url(url): 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'} req = requests.get(url=url, headers=headers) return req.content.decode('utf-8')def max_length(content): if len(content) > 4891: print("翻譯文本超過限制!") returndef print_result(parm): result = parm str_end = result.find("\",") if str_end > 4: print("翻譯的結果爲:",result[4:str_end])def en_to_zn_translate(content, tk): max_length(content) content = urllib.parse.quote(content) #英譯漢 url = "http://translate.google.cn/translate_a/single?client=t" \ "&sl=en&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&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" \ "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (tk, content) result = open_url(url) print_result(result)def zn_to_en_translate(content,tk): max_length(content) content = urllib.parse.quote(content) #漢譯英 url = "http://translate.google.cn/translate_a/single?client=t"\ "&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca"\ "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8"\ "&source=btn&ssel=3&tsel=3&kc=0&tk=%s&q=%s"%(tk,content) result = open_url(url) print_result(result)def main(): js = Return_tk() while True: print(termcolor.colored("請先輸入要進行的操做:q表示退出;e表示英文翻譯成中文;z表示中文翻譯成英文。","red")) change = input("請選擇翻譯選項:") if change == 'q': break elif change == 'e': content = input("請輸入要翻譯的英文:") tk = js.getTk(content) en_to_zn_translate(content,tk) elif change == 'z': content = input("請輸入要翻譯的中文:") tk = js.getTk(content) zn_to_en_translate(content,tk) else: print("請輸入正確的選項!") main()
總結:函數
從代碼上來看,寫的比較簡潔,沒有高大上的寫法,只是單純的實現一個功能。google
除了tk的生成封裝成了類,其餘的所有是用函數式編程實現。url