python 簡單的調(diào)用有道翻譯
代碼
import jsonimport requests# 翻譯函數(shù),word 需要翻譯的內(nèi)容def translate(word): # 有道詞典 api url = ’http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null’ # 傳輸?shù)膮?shù),其中 i 為需要翻譯的內(nèi)容 key = { ’type’: 'AUTO', ’i’: word, 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'ue': 'UTF-8', 'action': 'FY_BY_CLICKBUTTON', 'typoResult': 'true' } # key 這個字典為發(fā)送給有道詞典服務器的內(nèi)容 response = requests.post(url, data=key) # 判斷服務器是否相應成功 if response.status_code == 200: # 然后相應的結(jié)果 return response.text else: print('有道詞典調(diào)用失敗') # 相應失敗就返回空 return Nonedef get_reuslt(repsonse): # 通過 json.loads 把返回的結(jié)果加載成 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(’魚’))''''''
運行效果:
以上就是python 簡單的調(diào)用有道翻譯的詳細內(nèi)容,更多關(guān)于python 調(diào)用有道翻譯的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. vue實現(xiàn)web在線聊天功能2. Django使用HTTP協(xié)議向服務器傳參方式小結(jié)3. Java Bean與Map之間相互轉(zhuǎn)化的實現(xiàn)方法4. SpringBoot+TestNG單元測試的實現(xiàn)5. Springboot 全局日期格式化處理的實現(xiàn)6. Java使用Tesseract-Ocr識別數(shù)字7. Python使用urlretrieve實現(xiàn)直接遠程下載圖片的示例代碼8. docker容器調(diào)用yum報錯的解決辦法9. JAMon(Java Application Monitor)備忘記10. 完美解決vue 中多個echarts圖表自適應的問題
