1. 谷歌翻译 API(需要设置代理)

  以翻译 “Authentication Architecture” 为例

1.1 网页调用

1.1.1 通过浏览器或命令行调用

curl "https://translate.google.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=zh_CN&q=Authentication%20Architecture" -H "Accept: application/json" --proxy http://127.0.0.1:7890

1.1.2 返回结果

{
"sentences": [
{
"trans": "认证架构",
"orig": "Authentication Architecture",
"backend": 3,
"model_specification": [{}],
"translation_engine_debug_info": [
{
"model_tracking": {
"checkpoint_md5": "95ad550c486923f3a292df3e4cffc30f",
"launch_doc": "en_zh_2022q2.md"
}
}
]
}
],
"src": "en",
"confidence": 1.0,
"spell": {},
"ld_result": {
"srclangs": ["en"],
"srclangs_confidences": [1.0],
"extended_srclangs": ["en"]
}
}

1.2 通过 python 调用

def translate_from_google(txt: str) -> str:
proxies = {
"http": "http://127.0.0.1:7890",
"https": "http://127.0.0.1:7890",
}
txt = urllib.parse.quote(txt, safe=' ,=&/+()')
url = 'https://translate.google.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=zh_CN&q='+txt
res = requests.get(url=url, proxies=proxies)
return res.json().get('sentences')[0].get('trans') # 谷歌
translate_from_google('Authentication Architecture') # 调用
# 输出"认证架构"

2. 有道翻译 API

2.1 网页调用

2.1.1 通过浏览器或命令行调用

curl "https://fanyi.youdao.com/translate?doctype=json&type=EN2ZH_CN&i=Authentication%20Architecture" -H "Accept: application/json"

2.1.2 返回的 json 结果

{
"type": "EN2ZH_CN",
"errorCode": 0,
"elapsedTime": 1,
"translateResult": [
[{ "src": "Authentication Architecture", "tgt": "认证体系结构" }]
]
}

2.2 通过 python 调用

def translate_from_youdao(txt:str)->str:
txt = urllib.parse.quote(txt, safe=' ,=&/+()')
url = 'https://fanyi.youdao.com/translate?&doctype=json&type=EN2ZH_CN&i='+txt
res = requests.get(url=url,proxies=proxies)
return res.json().get('translateResult')[0][0].get('tgt') # 有道
translate_from_youdao('Authentication Architecture') # 调用
# 输出"认证体系结构"