python 調(diào)用Google翻譯接口的方法
一、網(wǎng)頁(yè)分析
打開(kāi)谷歌翻譯鏈接: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)稱(chēng)。
二、代碼演示
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import re
def 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)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python各種擴(kuò)展名區(qū)別點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于Python各種擴(kuò)展名區(qū)別點(diǎn)整理,需要的朋友們可以學(xué)習(xí)下。2020-02-02
Python實(shí)現(xiàn)自動(dòng)化批量調(diào)整Word樣式
在日常工作中,處理大量的Word文檔是一個(gè)常見(jiàn)的任務(wù),尤其是需要批量修改文檔的樣式時(shí),本文為大家介紹了如何使用Python實(shí)現(xiàn)自動(dòng)化批量調(diào)整Word樣式,需要的可以參考下2024-12-12
Python SVM(支持向量機(jī))實(shí)現(xiàn)方法完整示例
這篇文章主要介紹了Python SVM(支持向量機(jī))實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了基于Python實(shí)現(xiàn)向量機(jī)SVM算法的具體步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-06-06
pycharm實(shí)現(xiàn)增加運(yùn)行時(shí)內(nèi)存
這篇文章主要介紹了pycharm實(shí)現(xiàn)增加運(yùn)行時(shí)內(nèi)存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Django配合python進(jìn)行requests請(qǐng)求的問(wèn)題及解決方法
Python作為目前比較流行的編程語(yǔ)言,他內(nèi)置的Django框架就是一個(gè)很好的網(wǎng)絡(luò)框架,可以被用來(lái)搭建后端,和前端進(jìn)行交互,那么我們現(xiàn)在來(lái)學(xué)習(xí)一下,如何用Python本地進(jìn)行requests請(qǐng)求,并通過(guò)請(qǐng)求讓Django幫我們解決一些問(wèn)題2022-06-06
python利用 keyboard 庫(kù)記錄鍵盤(pán)事件
這篇文章主要介紹了python利用 keyboard 庫(kù)記錄鍵盤(pán)事件,幫助大家更好的利用python進(jìn)行辦公,感興趣的朋友可以了解下2020-10-10

