python 調(diào)用Google翻譯接口的方法
一、網(wǎng)頁分析
打開谷歌翻譯鏈接:https://translate.google.com/
按F12,點(diǎn)擊network。在左側(cè)輸入"who are you"
可以看到,請求的鏈接為:
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 表示日本的簡稱。
二、代碼演示
# !/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" } # 請求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è)國家語言 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翻譯接口的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pandas參數(shù)設(shè)置的實(shí)用小技巧
這篇文章主要給大家介紹了關(guān)于pandas參數(shù)設(shè)置的實(shí)用小技巧,文中通過實(shí)例代碼結(jié)束的非常詳細(xì),對大家學(xué)習(xí)或者使用pandas具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08對python使用http、https代理的實(shí)例講解
今天小編就為大家分享一篇對python使用http、https代理的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05聊聊pytorch中Optimizer與optimizer.step()的用法
這篇文章主要介紹了pytorch中Optimizer與optimizer.step()的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python圖像處理之直線和曲線的擬合與繪制【curve_fit()應(yīng)用】
這篇文章主要介紹了Python圖像處理之直線和曲線的擬合與繪制,結(jié)合實(shí)例形式分析了Python曲線擬合相關(guān)函數(shù)curve_fit()的使用技巧,需要的朋友可以參考下2018-12-12Python?一篇文章看懂Python集合與字典數(shù)據(jù)類型
集合并不是一種數(shù)據(jù)處理類型,而是一種中間類型。集合(set)是一個(gè)無序、不重復(fù)的元素序列,經(jīng)常被用來處理兩個(gè)列表進(jìn)行交并差的處理性。本文將詳細(xì)講解集合的一些常用方法,感興趣的可以了解一下2022-03-03Python繼承和子類從Person到Student演示實(shí)例
這篇文章主要為大家介紹了Python繼承和子類從Person到Student演示實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10使用 Python 處理3萬多條數(shù)據(jù)只要幾秒鐘
在工作中經(jīng)常遇到大量的數(shù)據(jù)需要整合、去重、按照特定格式導(dǎo)出等情況。這篇文章主要介紹了使用 Python 處理3萬多條數(shù)據(jù)只要幾秒鐘的相關(guān)知識,需要的朋友可以參考下2020-01-01