破解google翻譯接口


一、網頁分析

打開谷歌翻譯鏈接:https://translate.google.com/

按F12,點擊network。在左側輸入"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

 

發送的數據為:

這里面的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"
    # 數據參數
    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:
            # 正則匹配結果
            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

# 翻譯各個國家語言
for i in ['en', 'zh', 'fr', 'ja', 'de']:
    response = translated_content("who are you", i)
    print(response)
View Code

執行輸出:

who are you
你是誰
qui êtes vous
あなたは誰
Wer bist du

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM