python 簡(jiǎn)單的調(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 這個(gè)字典為發(fā)送給有道詞典服務(wù)器的內(nèi)容 response = requests.post(url, data=key) # 判斷服務(wù)器是否相應(yīng)成功 if response.status_code == 200: # 然后相應(yīng)的結(jié)果 return response.text else: print('有道詞典調(diào)用失敗') # 相應(yīng)失敗就返回空 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(’魚’))''''''
運(yùn)行效果:
以上就是python 簡(jiǎn)單的調(diào)用有道翻譯的詳細(xì)內(nèi)容,更多關(guān)于python 調(diào)用有道翻譯的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python網(wǎng)絡(luò)編程之ZeroMQ知識(shí)總結(jié)2. chat.asp聊天程序的編寫方法3. Python查找算法之分塊查找算法的實(shí)現(xiàn)4. .NET 中配置從xml轉(zhuǎn)向json方法示例詳解5. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例6. python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計(jì)處理操作7. Python TestSuite生成測(cè)試報(bào)告過程解析8. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題9. JSP之表單提交get和post的區(qū)別詳解及實(shí)例10. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案
