python调用谷歌翻译_python调用谷歌翻译

万俟浩
2023-12-01

2019-05-09

3229

在平时使用谷歌翻译的过程中,经常会遇到需要批量翻译大量文本的情景,这种时候需要调用谷歌翻译的API

首先可以使用python库googletranspip install googletrans#使用方法from googletrans import Translator

translator = Translator(service_urls=['translate.google.cn'])

source = '我还是不开心!'text = translator.translate(source,src='zh-cn',dest='en').text

print(text)"i'm still not happy!"

但是在面对大规模需要翻译的句子时就会很慢,所以可以使用协程的方法。

这里我们使用了基于gevents库的grequests库。

仔细看了下googletrans库的核心代码,发现主要是构造一个url,然后发起get请求,得到一个json的结果,从中提取出翻译结果。

构造url的过程需要一个token,根据某些规则生成,所以为了方便还是调用googletrans的部分函数。

具体的参考代码如下:

这里是将德语(de)翻译成英语(en),代码中需要相应修改。import grequestsimport loggingimport jsonfrom googletrans import Translatorfrom googletrans.utils import format_json

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}

translator = Translator(service_urls=['translate.google.cn'])

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',filename='log.txt')

logger = logging.getLogger()def exception_handler(request, exception):

logger.warning('exception when at %s :%s',request.url,exception)def work(urls):

reqs = (grequests.get(u,verify=True, allow_redirects=True, timeout=4) for u in urls)

res = grequests.map(reqs, exception_handler=exception_handler,size=20) return resdef totaltranslate():

file2 = open('de2en_en.txt',mode='a',encoding='utf-8') with open('de.txt',mode='r',encoding='utf-8') as f:

urls = []

num = 0

for line in f:

num+=1

line = line.strip()

token = translator.token_acquirer.do(line)

url="https://translate.google.cn/translate_a/single?client=t&sl=de&tl=en&hl=en&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&ssel=3&tsel=0&kc=1&tk={0}&q={1}".format(token,line)

urls.append(url) if len(urls) >= 50:

res = work(urls) for r in res: if hasattr(r,'status_code'): if r.status_code == 200: try:

a=format_json(r.text)

target = ''.join([d[0] if d[0] else '' for d in a[0]])

source = ''.join([d[1] if d[1] else '' for d in a[0]]) except Exception as e:

logger.error('when format:%s',e)

logger.error('%s\n%s',r.text)

source = ''

target = ''

if len(source) != 0 and len(target) != 0:

file2.write(target+'\n') else:

file2.write('\n') else:

file2.write('\n')

urls = []

logger.info('finish 50 sentence, now at %s',num)

file2.close()def sentencetranslate(line):

line = line.strip()

text = translator.translate(line,src='de',dest='en').text return textdef completetranslate():

file1 = open('de2en_en.txt',mode='r',encoding='utf-8')

file2 = open('new_de2en_en.txt',mode='a',encoding='utf-8')

i = 1

with open('de.txt',mode='r',encoding='utf-8') as f: for line in f:

t = file1.readline() if len(t) == 1:#'only \n'

text = sentencetranslate(line)

file2.write(text+'\n') else:

file2.write(t)

i += 1

if i%100 == 0:

print(i)

file1.close()

file2.close()if name == "main":

totaltranslate()

completetranslate()

totaltranslate()以及翻译了大部分的句子,但是可能因为某些原因有一些句子没有翻译,所以还需要completetranslate()来补全翻译结果。

在grequests.map()中size设置为20的情况下,平均每秒翻译20句。

 类似资料: