3 calling methods of Google multilingual translation interface (Python)

2022.10.01 update:

Since Google Translate has stopped serving in China, please move to the Youdao translation interface: Youdao translation multilingual interface calling method (Python) In our daily life and work, we often need to use translation tools. Since Google Translate has stopped serving in China, Youdao Translate has been used as a substitute, and now there are two ways to call Youdao Translate interface through Python. https://blog.csdn.net/qq_40039731/article/details/127302928

        At the end of September, Google Translate, a webpage translation tool owned by the search giant Google, stopped its service in China. Now, when you visit the translate.google.cn webpage, it will point to the Google Hong Kong station. As early as 2010, after Google announced its withdrawal from China, only two services, Google Translate and Google Maps, were retained. From February 3, 2020, Google Maps will stop serving in China, and now, the last Google translation service is also closed.
        It is understood that many users have reported on the Reddit website that Google Translate cannot be used normally in China. Later, a Google spokesperson replied to the foreign media TechCrunch by email, acknowledging that Google Translate service was stopped in China, and stated that the reason was "usage rate too low".

original:

        Google Translate is a free translation service that supports instant translation between up to 133 languages. In addition, Google Translate uses Statistical Machine Translation (SMT), which will conduct a large number of searches and speculations in human-translated documents during the translation process, and use this to obtain the most suitable translation results.

        In our daily life and work, we often need to use translation tools. Among many translation tools, I personally prefer Google Translate, so here are three ways to call the Google Translate interface through Python.

 method one

import requests
def translate_1(text, src_lang='auto', to_lang='en'):
    googleapis_url = 'https://translate.googleapis.com/translate_a/single'
    url = '%s?client=gtx&sl=%s&tl=%s&dt=t&q=%s' % (googleapis_url,src_lang,to_lang,text)
    data = requests.get(url).json()
    res = ''.join([s[0] for s in data[0]])
    return res

Method Two

# pip install bs4
import requests
from bs4 import BeautifulSoup
def translate_2(text, src_lang='auto', to_lang='en'):
    url = 'https://translate.google.cn/m?q=%s&tl=%s&sl=%s' % (text,to_lang,src_lang)
    soup = BeautifulSoup(requests.get(url).text,'lxml')
    res = soup.find('div',{'class':'result-container'}).text
    return res

method three

# pip install googletrans==4.0.0-rc1
from googletrans import Translator
def translate_3(text, src_lang='auto', to_lang='en'):
    translator = Translator(service_urls=['translate.google.cn'])
    res = translator.translate(text, dest=to_lang, src=src_lang).text
    return res

Display of running results

 Get all languages ​​and their corresponding encodings

import requests
from bs4 import BeautifulSoup
def get_all_langcode():
    response = requests.get(url='https://translate.google.cn')
    soup = BeautifulSoup(response.text,'lxml')
    res_dict = {}
    for res in soup.find_all('div',{'class':'qSb8Pe'}):
        res_dict[res['data-language-code']] = res.find('div',{'class':'Llmcnf'}).text
    return res_dict
{
 'auto': '检测语言',
 'sq': '阿尔巴尼亚语',
 'ar': '阿拉伯语',
 'am': '阿姆哈拉语',
 'as': '阿萨姆语',
 'az': '阿塞拜疆语',
 'ee': '埃维语',
 'ay': '艾马拉语',
 'ga': '爱尔兰语',
 'et': '爱沙尼亚语',
 'or': '奥利亚语',
 'om': '奥罗莫语',
 'eu': '巴斯克语',
 'be': '白俄罗斯语',
 'bm': '班巴拉语',
 'bg': '保加利亚语',
 'is': '冰岛语',
 'pl': '波兰语',
 'bs': '波斯尼亚语',
 'fa': '波斯语',
 'bho': '博杰普尔语',
 'af': '布尔语(南非荷兰语)',
 'tt': '鞑靼语',
 'da': '丹麦语',
 'de': '德语',
 'dv': '迪维希语',
 'ti': '蒂格尼亚语',
 'doi': '多格来语',
 'ru': '俄语',
 'fr': '法语',
 'sa': '梵语',
 'tl': '菲律宾语',
 'fi': '芬兰语',
 'fy': '弗里西语',
 'km': '高棉语',
 'ka': '格鲁吉亚语',
 'gom': '贡根语',
 'gu': '古吉拉特语',
 'gn': '瓜拉尼语',
 'kk': '哈萨克语',
 'ht': '海地克里奥尔语',
 'ko': '韩语',
 'ha': '豪萨语',
 'nl': '荷兰语',
 'ky': '吉尔吉斯语',
 'gl': '加利西亚语',
 'ca': '加泰罗尼亚语',
 'cs': '捷克语',
 'kn': '卡纳达语',
 'co': '科西嘉语',
 'kri': '克里奥尔语',
 'hr': '克罗地亚语',
 'qu': '克丘亚语',
 'ku': '库尔德语(库尔曼吉语)',
 'ckb': '库尔德语(索拉尼)',
 'la': '拉丁语',
 'lv': '拉脱维亚语',
 'lo': '老挝语',
 'lt': '立陶宛语',
 'ln': '林格拉语',
 'lg': '卢干达语',
 'lb': '卢森堡语',
 'rw': '卢旺达语',
 'ro': '罗马尼亚语',
 'mg': '马尔加什语',
 'mt': '马耳他语',
 'mr': '马拉地语',
 'ml': '马拉雅拉姆语',
 'ms': '马来语',
 'mk': '马其顿语',
 'mai': '迈蒂利语',
 'mi': '毛利语',
 'mni-Mtei': '梅泰语(曼尼普尔语)',
 'mn': '蒙古语',
 'bn': '孟加拉语',
 'lus': '米佐语',
 'my': '缅甸语',
 'hmn': '苗语',
 'xh': '南非科萨语',
 'zu': '南非祖鲁语',
 'ne': '尼泊尔语',
 'no': '挪威语',
 'pa': '旁遮普语',
 'pt': '葡萄牙语',
 'ps': '普什图语',
 'ny': '齐切瓦语',
 'ak': '契维语',
 'ja': '日语',
 'sv': '瑞典语',
 'sm': '萨摩亚语',
 'sr': '塞尔维亚语',
 'nso': '塞佩蒂语',
 'st': '塞索托语',
 'si': '僧伽罗语',
 'eo': '世界语',
 'sk': '斯洛伐克语',
 'sl': '斯洛文尼亚语',
 'sw': '斯瓦希里语',
 'gd': '苏格兰盖尔语',
 'ceb': '宿务语',
 'so': '索马里语',
 'tg': '塔吉克语',
 'te': '泰卢固语',
 'ta': '泰米尔语',
 'th': '泰语',
 'tr': '土耳其语',
 'tk': '土库曼语',
 'cy': '威尔士语',
 'ug': '维吾尔语',
 'ur': '乌尔都语',
 'uk': '乌克兰语',
 'uz': '乌兹别克语',
 'es': '西班牙语',
 'iw': '希伯来语',
 'el': '希腊语',
 'haw': '夏威夷语',
 'sd': '信德语',
 'hu': '匈牙利语',
 'sn': '修纳语',
 'hy': '亚美尼亚语',
 'ig': '伊博语',
 'ilo': '伊洛卡诺语',
 'it': '意大利语',
 'yi': '意第绪语',
 'hi': '印地语',
 'su': '印尼巽他语',
 'id': '印尼语',
 'jw': '印尼爪哇语',
 'en': '英语',
 'yo': '约鲁巴语',
 'vi': '越南语',
 'zh-TW': '中文(繁体)',
 'zh-CN': '中文(简体)',
 'ts': '宗加语'
}

Guess you like

Origin blog.csdn.net/qq_40039731/article/details/126239369