有道詞典的翻譯接口


import json

import requests

# 翻譯函數,word 需要翻譯的內容
def translate(word):
# 有道詞典 api
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
# 傳輸的參數,其中 i 為需要翻譯的內容
key = {
'type': "AUTO",
'i': word,
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"ue": "UTF-8",
"action": "FY_BY_CLICKBUTTON",
"typoResult": "true"
}
# key 這個字典為發送給有道詞典服務器的內容
response = requests.post(url, data=key)
# 判斷服務器是否相應成功
if response.status_code == 200:
# 然后相應的結果
print('111')
return response.text
else:
print("有道詞典調用失敗")
# 相應失敗就返回空
return None

def get_reuslt(repsonse):
# 通過 json.loads 把返回的結果加載成 json 格式
result = json.loads(repsonse)

return result['translateResult'][0][0]['tgt']

def main(err):
word = err
list_trans = translate(word)
return get_reuslt(list_trans)

print(main('SyntaxError: bad input on line 1'))


免責聲明!

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



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