免費呼叫谷歌翻譯

2021-08-20 11:22:26 字數 1415 閱讀 7484

最近公司有需求,需要呼叫谷歌翻譯的介面。雖然我最後可以勉強實現了翻譯的功能,但還是把這個方法記錄下來供各位參考。

首先宣告,我這種實現方法只能翻譯少量的內容,如果想要翻譯比較多的內容就只能把內容拆分開來翻譯。

1.找到谷歌翻譯,通過f12檢視谷歌翻譯的時候呼叫的介面:

解析開來就是:client=t、sl=auto、tl=tolanguage、hl=fromlanguage、da=at、dt=bd、dt=ex、dt=ld、dt=gca、dt=rw、dt=rm、dt=ss、dt=t、ie=utf8、oe=utf8、ssel=0、tsel=3、kc=0、tk=(需要經過js檔案的計算)、q=(需要翻譯的文字)

這麼多引數中只有tk引數最麻煩,需要使用經過js檔案的計算得到,js檔案我會在文章最後面分享。

2.使用jsoup模擬瀏覽器傳送請求,接收到返回資訊並解析返回資訊,得到翻譯的結果。(注意:一定要給請求中間加一條sleep(1000),否則谷歌可能會封你得ip)

//谷歌翻譯(只能翻譯比較短的內容)get

public static string translategoogleget(string transaltecontente,string fromlanguage,string tolanguage) catch (unsupportedencodingexception e1)

//獲取請求連線

try catch (interruptedexception e1)

string resp;

try

string string = resp.split("]],")[0];

string split = string.split(",null,null,3],");

for (string string2 : split)

}/*int start = resp.indexof("\"")+1;

int end = resp.indexof("\"", start+1);*/

/*string split = resp.split("]],")[0].replace("[[[", "[").split("],");

for (string string : split)

}*/return result.tostring();

} catch (exception e)

return "";

}

/**

* 獲取谷歌翻譯的tk值

* @param text

* @return

*/private static string googletoken(string text)

} catch (exception e)

return tk;

} }

python 呼叫谷歌翻譯

coding utf 8 import requests import json from fake useragent import useragent from bs4 import beautifulsoup import execjs 必須,需要先用pip 安裝,用來執行js指令碼 clas...

免費試用谷歌的翻譯介面

最近做個東西,需將各種語言翻譯成中文,看了各家的翻譯效果,還是谷歌的最好。但谷歌的未提供免費介面,研究了谷歌的翻譯頁面,輸入內容後會觸發ajax請求,請求引數中除了輸入內容,還有個加密引數tk,該加密演算法在壓縮的js 中,我也在網上找到了摘出來的 js格式,一大段,壓縮 翻譯起來很吃力,遂未翻譯,...

python使用谷歌翻譯

俺寫指令碼向來喜歡直接copy然後修改,看網上使用谷歌翻譯api還要自己去破解的,這不是直接造輪子麼。所以我選擇安裝googletrans 模組。直接 pip install googletrans 就完事了。實際操作發現不用 不行,我想應該是host問題,看了原始碼之後發現加引數service u...