本人有三种调用谷歌翻译接口的方法,喜欢哪种用哪种
方法一:最简单
安装
pip install google_trans_new
google翻译
from google_trans_new import google_translator
ft = google_translator(timeout=10)
text = 'hello world!'
lang = 'zh-cn
translate_text = t.translate(text, lang)
print(translate_text)
方法二 提取了google_trans_new源代码自定义的
import json, requests, random, re
from urllib.parse import quote
import urllib3
import logging
LANGUAGES = {
'af': 'afrikaans',
'sq': 'albanian',
'am': 'amharic',
'ar': 'arabic',
'hy': 'armenian',
'az': 'azerbaijani',
'eu': 'basque',
'be': 'belarusian',
'bn': 'bengali',
'bs': 'bosnian',
'bg': 'bulgarian',
'ca': 'catalan',
'ceb': 'cebuano',
'ny': 'chichewa',
'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)',
'co': 'corsican',
'hr': 'croatian',
'cs': 'czech',
'da': 'danish',
'nl': 'dutch',
'en': 'english',
'eo': 'esperanto',
'et': 'estonian',
'tl': 'filipino',
'fi': 'finnish',
'fr': 'french',
'fy': 'frisian',
'gl': 'galician',
'ka': 'georgian',
'de': 'german',
'el': 'greek',
'gu': 'gujarati',
'ht': 'haitian creole',
'ha': 'hausa',
'haw': 'hawaiian',
'iw': 'hebrew',
'he': 'hebrew',
'hi': 'hindi',
'hmn': 'hmong',
'hu': 'hungarian',
'is': 'icelandic',
'ig': 'igbo',
'id': 'indonesian',
'ga': 'irish',
'it': 'italian',
'ja': 'japanese',
'jw': 'javanese',
'kn': 'kannada',
'kk': 'kazakh',
'km': 'khmer',
'ko': 'korean',
'ku': 'kurdish (kurmanji)',
'ky': 'kyrgyz',
'lo': 'lao',
'la': 'latin',
'lv': 'latvian',
'lt': 'lithuanian',
'lb': 'luxembourgish',
'mk': 'macedonian',
'mg': 'malagasy',
'ms': 'malay',
'ml': 'malayalam',
'mt': 'maltese',
'mi': 'maori',
'mr': 'marathi',
'mn': 'mongolian',
'my': 'myanmar (burmese)',
'ne': 'nepali',
'no': 'norwegian',
'or': 'odia',
'ps': 'pashto',
'fa': 'persian',
'pl': 'polish',
'pt': 'portuguese',
'pa': 'punjabi',
'ro': 'romanian',
'ru': 'russian',
'sm': 'samoan',
'gd': 'scots gaelic',
'sr': 'serbian',
'st': 'sesotho',
'sn': 'shona',
'sd': 'sindhi',
'si': 'sinhala',
'sk': 'slovak',
'sl': 'slovenian',
'so': 'somali',
'es': 'spanish',
'su': 'sundanese',
'sw': 'swahili',
'sv': 'swedish',
'tg': 'tajik',
'ta': 'tamil',
'te': 'telugu',
'th': 'thai',
'tr': 'turkish',
'tk': 'turkmen',
'uk': 'ukrainian',
'ur': 'urdu',
'ug': 'uyghur',
'uz': 'uzbek',
'vi': 'vietnamese',
'cy': 'welsh',
'xh': 'xhosa',
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu',
}
DEFAULT_SERVICE_URLS = ('translate.google.ac','translate.google.ad','translate.google.ae',
'translate.google.al','translate.google.am','translate.google.as',
'translate.google.at','translate.google.az','translate.google.ba',
'translate.google.be','translate.google.bf','translate.google.bg',
'translate.google.bi','translate.google.bj','translate.google.bs',
'translate.google.bt','translate.google.by','translate.google.ca',
'translate.google.cat','translate.google.cc','translate.google.cd',
'translate.google.cf','translate.google.cg','translate.google.ch',
'translate.google.ci','translate.google.cl','translate.google.cm',
'translate.google.cn','translate.google.co.ao','translate.google.co.bw',
'translate.google.co.ck','translate.google.co.cr','translate.google.co.id',
'translate.google.co.il','translate.google.co.in','translate.google.co.jp',
'translate.google.co.ke','translate.google.co.kr','translate.google.co.ls',
'translate.google.co.ma','translate.google.co.mz','translate.google.co.nz',
'translate.google.co.th','translate.google.co.tz','translate.google.co.ug',
'translate.google.co.uk','translate.google.co.uz','translate.google.co.ve',
'translate.google.co.vi','translate.google.co.za','translate.google.co.zm',
'translate.google.co.zw','translate.google.co','translate.google.com.af',
'translate.google.com.ag','translate.google.com.ai','translate.google.com.ar',
'translate.google.com.au','translate.google.com.bd','translate.google.com.bh',
'translate.google.com.bn','translate.google.com.bo','translate.google.com.br',
'translate.google.com.bz','translate.google.com.co','translate.google.com.cu',
'translate.google.com.cy','translate.google.com.do','translate.google.com.ec',
'translate.google.com.eg','translate.google.com.et','translate.google.com.fj',
'translate.google.com.gh','translate.google.com.gi','translate.google.com.gt',
'translate.google.com.hk','translate.google.com.jm','translate.google.com.kh',
'translate.google.com.kw','translate.google.com.lb','translate.google.com.lc',
'translate.google.com.ly','translate.google.com.mm','translate.google.com.mt',
'translate.google.com.mx','translate.google.com.my','translate.google.com.na',
'translate.google.com.ng','translate.google.com.ni','translate.google.com.np',
'translate.google.com.om','translate.google.com.pa','translate.google.com.pe',
'translate.google.com.pg','translate.google.com.ph','translate.google.com.pk',
'translate.google.com.pr','translate.google.com.py','translate.google.com.qa',
'translate.google.com.sa','translate.google.com.sb','translate.google.com.sg',
'translate.google.com.sl','translate.google.com.sv','translate.google.com.tj',
'translate.google.com.tr','translate.google.com.tw','translate.google.com.ua',
'translate.google.com.uy','translate.google.com.vc','translate.google.com.vn',
'translate.google.com','translate.google.cv','translate.google.cx',
'translate.google.cz','translate.google.de','translate.google.dj',
'translate.google.dk','translate.google.dm','translate.google.dz',
'translate.google.ee','translate.google.es','translate.google.eu',
'translate.google.fi','translate.google.fm','translate.google.fr',
'translate.google.ga','translate.google.ge','translate.google.gf',
'translate.google.gg','translate.google.gl','translate.google.gm',
'translate.google.gp','translate.google.gr','translate.google.gy',
'translate.google.hn','translate.google.hr','translate.google.ht',
'translate.google.hu','translate.google.ie','translate.google.im',
'translate.google.io','translate.google.iq','translate.google.is',
'translate.google.it','translate.google.je','translate.google.jo',
'translate.google.kg','translate.google.ki','translate.google.kz',
'translate.google.la','translate.google.li','translate.google.lk',
'translate.google.lt','translate.google.lu','translate.google.lv',
'translate.google.md','translate.google.me','translate.google.mg',
'translate.google.mk','translate.google.ml','translate.google.mn',
'translate.google.ms','translate.google.mu','translate.google.mv',
'translate.google.mw','translate.google.ne','translate.google.nf',
'translate.google.nl','translate.google.no','translate.google.nr',
'translate.google.nu','translate.google.pl','translate.google.pn',
'translate.google.ps','translate.google.pt','translate.google.ro',
'translate.google.rs','translate.google.ru','translate.google.rw',
'translate.google.sc','translate.google.se','translate.google.sh',
'translate.google.si','translate.google.sk','translate.google.sm',
'translate.google.sn','translate.google.so','translate.google.sr',
'translate.google.st','translate.google.td','translate.google.tg',
'translate.google.tk','translate.google.tl','translate.google.tm',
'translate.google.tn','translate.google.to','translate.google.tt',
'translate.google.us','translate.google.vg','translate.google.vu','translate.google.ws')
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
URLS_SUFFIX = [re.search('translate.google.(.*)', url.strip()).group(1) for url in DEFAULT_SERVICE_URLS]
URL_SUFFIX_DEFAULT = 'cn'
class google_new_transError(Exception):
"""Exception that uses context to present a meaningful error message"""
def __init__(self, msg=None, **kwargs):
self.tts = kwargs.pop('tts', None)
self.rsp = kwargs.pop('response', None)
if msg:
self.msg = msg
elif self.tts is not None:
self.msg = self.infer_msg(self.tts, self.rsp)
else:
self.msg = None
super(google_new_transError, self).__init__(self.msg)
def infer_msg(self, tts, rsp=None):
cause = "Unknown"
if rsp is None:
premise = "Failed to connect"
return "{}. Probable cause: {}".format(premise, "timeout")
else:
status = rsp.status_code
reason = rsp.reason
premise = "{:d} ({}) from TTS API".format(status, reason)
if status == 403:
cause = "Bad token or upstream API changes"
elif status == 200 and not tts.lang_check:
cause = "No audio stream in response. Unsupported language '%s'" % self.tts.lang
elif status >= 500:
cause = "Uptream API error. Try again later."
return "{}. Probable cause: {}".format(premise, cause)
class google_translator:
'''
You can use 108 language in target and source,details view LANGUAGES.
Target language: like 'en'、'zh'、'th'...
:param url_suffix: The source text(s) to be translated. Batch translation is supported via sequence input.
The value should be one of the url_suffix listed in : `DEFAULT_SERVICE_URLS`
:type url_suffix: UTF-8 :class:`str`; :class:`unicode`; string sequence (list, tuple, iterator, generator)
:param text: The source text(s) to be translated.
:type text: UTF-8 :class:`str`; :class:`unicode`;
:param lang_tgt: The language to translate the source text into.
The value should be one of the language codes listed in : `LANGUAGES`
:type lang_tgt: :class:`str`; :class:`unicode`
:param lang_src: The language of the source text.
The value should be one of the language codes listed in :const:`googletrans.LANGUAGES`
If a language is not specified,
the system will attempt to identify the source language automatically.
:type lang_src: :class:`str`; :class:`unicode`
:param timeout: Timeout Will be used for every request.
:type timeout: number or a double of numbers
:param proxies: proxies Will be used for every request.
:type proxies: class : dict; like: {'http': 'http:171.112.169.47:19934/', 'https': 'https:171.112.169.47:19934/'}
'''
def __init__(self, url_suffix="cn", timeout=5, proxies=None):
self.proxies = proxies
if url_suffix not in URLS_SUFFIX:
self.url_suffix = URL_SUFFIX_DEFAULT
else:
self.url_suffix = url_suffix
url_base = "https://translate.google.{}".format(self.url_suffix)
self.url = url_base + "/_/TranslateWebserverUi/data/batchexecute"
self.timeout = timeout
def _package_rpc(self, text, lang_src='auto', lang_tgt='auto'):
GOOGLE_TTS_RPC = ["MkEWBc"]
parameter = [[text.strip(), lang_src, lang_tgt, True], [1]]
escaped_parameter = json.dumps(parameter, separators=(',', ':'))
rpc = [[[random.choice(GOOGLE_TTS_RPC), escaped_parameter, None, "generic"]]]
espaced_rpc = json.dumps(rpc, separators=(',', ':'))
freq_initial = "f.req={}&".format(quote(espaced_rpc))
freq = freq_initial
return freq
def translate(self, text, lang_tgt='auto', lang_src='auto', pronounce=False):
try:
lang = LANGUAGES[lang_src]
except:
lang_src = 'auto'
try:
lang = LANGUAGES[lang_tgt]
except:
lang_src = 'auto'
text = str(text)
if len(text) >= 5000:
return "Warning: Can only detect less than 5000 characters"
if len(text) == 0:
return ""
headers = {
"Referer": "http://translate.google.{}/".format(self.url_suffix),
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/47.0.2526.106 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
}
freq = self._package_rpc(text, lang_src, lang_tgt)
response = requests.Request(method='POST',
url=self.url,
data=freq,
headers=headers,
)
try:
if self.proxies == None or type(self.proxies) != dict:
self.proxies = {}
with requests.Session() as s:
s.proxies = self.proxies
r = s.send(request=response.prepare(),
verify=False,
timeout=self.timeout)
for line in r.iter_lines(chunk_size=1024):
decoded_line = line.decode('utf-8')
if "MkEWBc" in decoded_line:
try:
response = (decoded_line + ']')
response = json.loads(response)
response = list(response)
response = json.loads(response[0][2])
response_ = list(response)
response = response_[1][0]
if len(response) == 1:
if len(response[0]) > 5:
sentences = response[0][5]
else: ## only url
sentences = response[0][0]
if pronounce == False:
return sentences
elif pronounce == True:
return [sentences, None, None]
translate_text = ""
for sentence in sentences:
sentence = sentence[0]
translate_text += sentence.strip() + ' '
translate_text = translate_text
if pronounce == False:
return translate_text
elif pronounce == True:
pronounce_src = (response_[0][0])
pronounce_tgt = (response_[1][0][0][1])
return [translate_text, pronounce_src, pronounce_tgt]
elif len(response) == 2:
sentences = []
for i in response:
sentences.append(i[0])
if pronounce == False:
return sentences
elif pronounce == True:
pronounce_src = (response_[0][0])
pronounce_tgt = (response_[1][0][0][1])
return [sentences, pronounce_src, pronounce_tgt]
except Exception as e:
raise e
r.raise_for_status()
except requests.exceptions.ConnectTimeout as e:
raise e
except requests.exceptions.HTTPError as e:
raise google_new_transError(tts=self, response=r)
except requests.exceptions.RequestException as e:
raise google_new_transError(tts=self)
def detect(self, text):
text = str(text)
if len(text) >= 5000:
return log.debug("Warning: Can only detect less than 5000 characters")
if len(text) == 0:
return ""
headers = {
"Referer": "http://translate.google.{}/".format(self.url_suffix),
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/47.0.2526.106 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
}
freq = self._package_rpc(text)
response = requests.Request(method='POST',
url=self.url,
data=freq,
headers=headers)
try:
if self.proxies == None or type(self.proxies) != dict:
self.proxies = {}
with requests.Session() as s:
s.proxies = self.proxies
r = s.send(request=response.prepare(),
verify=False,
timeout=self.timeout)
for line in r.iter_lines(chunk_size=1024):
decoded_line = line.decode('utf-8')
if "MkEWBc" in decoded_line:
try:
response = (decoded_line + ']')
response = json.loads(response)
response = list(response)
response = json.loads(response[0][2])
response = list(response)
detect_lang = response[0][2]
except Exception:
raise Exception
return [detect_lang, LANGUAGES[detect_lang.lower()]]
r.raise_for_status()
except requests.exceptions.HTTPError as e:
log.debug(str(e))
raise google_new_transError(tts=self, response=r)
except requests.exceptions.RequestException as e:
log.debug(str(e))
raise google_new_transError(tts=self)
def run(text, lang, proxy=None):
t = google_translator(timeout=20, proxies=proxy)
translate_text = t.translate(text, lang)
return translate_text
if __name__ == '__main__':
text = r"Circuit shows LT1568, Low Noise 4th Order Bandpass Filter (Single Supply Operation)"
t_text = run(text, 'zh-cn')
print(t_text)
方法三 结果为带**间隔的列表
# -*- coding: utf-8 -*-
import time
import execjs
import requests
from requests.exceptions import ConnectTimeout
from urllib3.exceptions import MaxRetryError
class Py4Js:
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 parse_translate(text):
if not text:
return None
content = ''
for dt in text[0][:-1]:
content += dt[0]
return content
def translate_content(content):
if content is None:
return None
translate_text = []
trans_text = lambda x: translate_text.append(parse_translate(translator(x)))
if len(content) > 4900:
text_en = content.split('**')
_t, _ts, n, indexs = '', [], 0, []
for i, v in enumerate(text_en):
_t += v
if len(_t) + (i - 1) * 2 > 4900:
n += 1
indexs.append(i)
_t = ''
for i in range(n + 1):
if i < n:
t1 = '**'.join(text_en[:indexs[i - 1]])
trans_text(t1)
else:
t2 = '**'.join(text_en[indexs[i - 1]:])
trans_text(text_en[t2])
translate_text = '**'.join(translate_text)
else:
trans_text(content)
translate_text = ''.join(translate_text)
translate_text = translate_text.split('**')
return translate_text
def translator(content):
if len(content) > 4900:
print("翻译的长度超过限制!")
return None
ii = 3
while ii > 0:
ii -= 1
try:
js = Py4Js()
tk = js.getTk(content)
param = {'tk': tk, 'q': content}
result = requests.post('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&otf=1&pc=1&ssel=0&tsel=0&kc=1',
params=param, proxies='', timeout=30)
if 400 <= result.status_code < 600:
raise ConnectionError
return result.json()
except (MaxRetryError, ConnectTimeout, ConnectionError, TypeError) as ee:
print(ee)
except Exception as e:
print(e, '---')
time.sleep(5)
return None
if __name__ == '__main__':
text_en = 'BEIJING - An editorial published on the website of New York Times has urged Japan not to try to rewrite its past by releasing a report on World War II sex slaves.'
t = translate_content(text_en)
print(t)
方法三如需在linux或者contOS环境下使用如果报
raise exceptions.RuntimeUnavailableError("Could not find an available JavaScript runtime.")
原因:
缺少可执行js的环境,解决办法,安装nodejs
解决:
yum install nodejs
yum install -y nodejs 报错问题及centos环境下nodejs安装解决方案
直接yum install -y nodejs会提示找不到nodejs这个模块
解决:安装nodesource后再执行yum install -y nodejs
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
或者
curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
然后
sudo yum -y install nodejs