python 調(diào)用Google翻譯接口的方法
一、網(wǎng)頁(yè)分析打開谷歌翻譯鏈接:https://translate.google.com/
按F12,點(diǎn)擊network。在左側(cè)輸入'who are you'
可以看到,請(qǐng)求的鏈接為:
https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&f.sid=-2609060161424095358&bl=boq_translate-webserver_20201203.07_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=359373&rt=c
發(fā)送的數(shù)據(jù)為:
這里面的who are you表示,需要翻譯的文字
ja 表示日本的簡(jiǎn)稱。
二、代碼演示
# !/usr/bin/python3# -*- coding: utf-8 -*-import requestsimport redef translated_content(text, target_language): headers = { 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', # 'accept-language': 'en,zh-CN;q=0.9,zh;q=0.8', 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36' } # 請(qǐng)求url url = 'https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&f.sid=-2609060161424095358&bl=boq_translate-webserver_20201203.07_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=359373&rt=c' # 數(shù)據(jù)參數(shù) from_data = { 'f.req': r'''[[['MkEWBc','[['{}','auto','{}',true],[null]]',null,'generic']]]'''.format(text, target_language) } try: r = requests.post(url, headers=headers, data=from_data, timeout=60) if r.status_code == 200: # 正則匹配結(jié)果 response = re.findall(r’,[['(.*?)',[’, r.text) if response:response = response[0] else:response = re.findall(r’,[['(.*?)']’, r.text)if response: response = response[0] return response except Exception as e: print(e) return False# 翻譯各個(gè)國(guó)家語(yǔ)言for i in [’en’, ’zh’, ’fr’, ’ja’, ’de’]: response = translated_content('who are you', i) print(response)
執(zhí)行輸出:
以上就是python 調(diào)用Google翻譯接口的方法的詳細(xì)內(nèi)容,更多關(guān)于python 調(diào)用Google翻譯接口的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案2. xml文件的結(jié)構(gòu)解讀第1/2頁(yè)3. jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法4. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐5. vue根據(jù)條件不同顯示不同按鈕的操作6. js中的正則表達(dá)式(一)7. IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整Git commit template 支持8. IntelliJ-Idea導(dǎo)出可執(zhí)行Jar流程解析9. 簡(jiǎn)單了解JavaScript作用域10. ASP程序中常用的腳本語(yǔ)言
