其他分享
首页 > 其他分享> > google translate api post调用

google translate api post调用

作者:互联网

使用python调用google 翻译api 

def 参数:(要翻译的文字,目标语言默认中文)

proxies改成自己的,若不需要可删除

key= '你的KEY'

def googleTranslate(text, target='zh'):
    import requests

    url = 'https://translation.googleapis.com/language/translate/v2'

    data = {
        'target': target,
        'key': key,
        'q': text,
    }
    lt = []
    for k, v in data.items():
        lt.append(k + '=' + str(v))
    query_string = '&'.join(lt)
    url = url + '?' + query_string
    print(url)

    proxies = {
        'http': 'http://127.0.0.1:7890',
        'https': 'http://127.0.0.1:7890',
    }

    result = requests.post(url, proxies=proxies).json()
    return result['data']['translations'][0]['translatedText']



if __name__ == '__main__':
    a=googleTranslate('You use curl and the body content from request.json to send the request to the Vision API. Enter the following on your command line:')
    print(a)

标签:__,proxies,http,url,lt,api,key,post,translate
来源: https://blog.csdn.net/m0_59859442/article/details/119271422