网易词典翻译
import urllib.request import urllib.parse import json while True: url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" data = {} data['i'] = input("请输入需要翻译的东西:") data['from'] = 'AUTO' data['to'] = 'AUTO' data['smartresult'] = 'dict' data['client'] = 'fanyideskweb' data['salt'] = '15476224241590' data['sign'] = '483fc67fc2bc252fcf81147f1ab6b3ed' data['ts'] = '1547622424159' data['bv'] = '87d648e10a4c1b783bfbb388b232ba69' data['doctype'] = 'json' data['version'] = '2.1' data['keyfrom'] = 'fanyi.web' data['action'] = 'FY_BY_REALTIME' data['typoResult'] = 'false' data = urllib.parse.urlencode(data).encode("utf-8") response = urllib.request.urlopen(url, data) html = response.read().decode("utf-8") page = json.loads(html) print(page['translateResult'][0][0]['tgt'])
百度翻译
# -*- coding: utf-8 -*- import re import time import execjs # pip3.8 install PyExecJS import pymysql import requests MYSQL_POST = 6033 MYSQL_USER = 'cloud_joy_sec' MYSQL_HOST = '192.168.209.26' MYSQL_PASSWORD = 'cloud_joy_sec' JS_CODE = """ function a(r, o) { for (var t = 0; t < o.length - 2; t += 3) { var a = o.charAt(t + 2); a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a), a = "+" === o.charAt(t + 1) ? r >>> a: r << a, r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a } return r } var C = null; var token = function(r, _gtk) { var o = r.length; o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substring(r.length, r.length - 10)); var t = void 0, t = null !== C ? C: (C = _gtk || "") || ""; for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) { var m = r.charCodeAt(g); 128 > m ? d[f++] = m: (2048 > m ? d[f++] = m >> 6 | 192 : (55296 === (64512 & m) && g + 1 < r.length && 56320 === (64512 & r.charCodeAt(g + 1)) ? (m = 65536 + ((1023 & m) << 10) + (1023 & r.charCodeAt(++g)), d[f++] = m >> 18 | 240, d[f++] = m >> 12 & 63 | 128) : d[f++] = m >> 12 | 224, d[f++] = m >> 6 & 63 | 128), d[f++] = 63 & m | 128) } for (var S = h, u = "+-a^+6", l = "+-3^+b+-f", s = 0; s < d.length; s++) S += d[s], S = a(S, u); return S = a(S, l), S ^= i, 0 > S && (S = (2147483647 & S) + 2147483648), S %= 1e6, S.toString() + "." + (S ^ h) } """ class Dict: def __init__(self): self.sess = requests.Session() self.headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' } self.token = None self.gtk = None self.javascript = execjs.compile(JS_CODE) # 获得token和gtk # 必须要加载两次保证token是最新的,否则会出现998的错误 self.loadMainPage() self.loadMainPage() def loadMainPage(self): """ load main page : https://fanyi.baidu.com/ and get token, gtk """ url = 'https://fanyi.baidu.com' try: r = self.sess.get(url, headers=self.headers) self.token = re.findall(r"token: '(.*?)',", r.text)[0] self.gtk = re.findall(r"window.gtk = '(.*?)';", r.text)[0] except Exception as e: raise e def langdetect(self, query): """ post query to https://fanyi.baidu.com/langdetect return json like {"error":0,"msg":"success","lan":"en"} """ url = 'https://fanyi.baidu.com/langdetect' data = {'query': query} try: r = self.sess.post(url=url, data=data) except Exception as e: raise e json = r.json() if 'msg' in json and json['msg'] == 'success': return json['lan'] return None def dictionary(self, query, dst='zh', src=None): """ get translate result from https://fanyi.baidu.com/v2transapi """ url = 'https://fanyi.baidu.com/v2transapi' sign = self.javascript.call('token', query, self.gtk) if not src: src = self.langdetect(query) data = { 'from': src, 'to': dst, 'query': query, 'simple_means_flag': 3, 'sign': sign, 'token': self.token, } try: r = self.sess.post(url=url, data=data) except Exception as e: raise e if r.status_code == 200: json = r.json() if 'error' in json: raise Exception('baidu sdk error: {}'.format(json['error'])) # 998错误则意味需要重新加载主页获取新的token return json return None if __name__ == "__main__": d = Dict() # open fanyi.baidu.com to get the code of language # https://fanyi.baidu.com/#zh/en/你好 db = pymysql.connect(host=MYSQL_HOST, port=MYSQL_POST, database="cloud_joy_monitoring", user=MYSQL_USER, password=MYSQL_PASSWORD, charset='utf8', autocommit=True) cursor = db.cursor() cursor.execute("select id, name, name_ydao, name_google, name_baidu from wemod_cheats order by id") for item in cursor.fetchall(): print(item) if not item[4]: json = d.dictionary(item[1], dst='zh', src='en') name_zh = json['trans_result']['data'][0]['dst'] print(name_zh) update_sql = 'UPDATE wemod_cheats set name_baidu="{}" WHERE id={}'.format(name_zh, item[0]) cursor.execute(update_sql) time.sleep(3) # json = d.dictionary('Max Attitude', dst='zh', src='en') # print(json['trans_result']['data'][0]['dst']) # json = d.dictionary('青花瓷', dst='en', src='zh') # print(json['trans_result']['data'][0]['dst'])
google翻译
import re import html from urllib import parse import requests GOOGLE_TRANSLATE_URL = 'http://translate.google.cn/m?q=%s&tl=%s&sl=%s' def translate(text, to_language="auto", text_language="auto"): text = parse.quote(text) url = GOOGLE_TRANSLATE_URL % (text,to_language,text_language) response = requests.get(url) data = response.text expr = r'(?s)class="(?:t0|result-container)">(.*?)<' result = re.findall(expr, data) if (len(result) == 0): return "" return html.unescape(result[0]) print(translate("你吃饭了么?", "en","zh-CN")) #汉语转英语 print(translate("你吃饭了么?", "ja","zh-CN")) #汉语转日语 print(translate("about your situation", "zh-CN","en")) #英语转汉语
from pygoogletranslation import Translator # pip install googletrans==4.0.0-rc1 proxy = {"http": "127.0.0.1:7890", "https": "127.0.0.1:7890"} translator = Translator(proxies=proxy) t = translator.translate("W Magazine is a leading destination where the worlds of fashion, film, art, and entertainment converge. Unbound by convention", dest='zh-CN') print(t.text)