import jsonimport 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 Nonedef 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'))