国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 調(diào)用Google翻譯接口的方法

瀏覽:2日期:2022-07-02 17:13:33

一、網(wǎng)頁(yè)分析打開(kāi)谷歌翻譯鏈接:https://translate.google.com/

按F12,點(diǎn)擊network。在左側(cè)輸入'who are you'

python 調(diào)用Google翻譯接口的方法

可以看到,請(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ù)為:

python 調(diào)用Google翻譯接口的方法

這里面的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翻譯接口的方法

以上就是python 調(diào)用Google翻譯接口的方法的詳細(xì)內(nèi)容,更多關(guān)于python 調(diào)用Google翻譯接口的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 凤庆县| 鹿泉市| 广东省| 通辽市| 临沧市| 麻阳| 大厂| 榆中县| 安陆市| 丰镇市| 炉霍县| 广州市| 延长县| 太仓市| 泸溪县| 白银市| 济源市| 定州市| 梨树县| 兰考县| 耿马| 阳曲县| 兴海县| 通辽市| 都匀市| 阿图什市| 新晃| 安宁市| 伊春市| 平顺县| 左权县| 新营市| 四会市| 山阴县| 那坡县| 论坛| 安岳县| 扎囊县| 长宁县| 喀喇沁旗| 格尔木市|