python單詞翻譯 python 中英文翻譯

2021-10-10 03:53:50 字數 1503 閱讀 1832

【例項簡介】

【例項截圖】

【核心**】import urllib.request #匯入urllib模組,主要功能是獲取網頁資料,request用於模擬傳送請求

import urllib.parse #parse提供url處理

import json #匯入json模組,是一種輕量級的資料交換格式,現在也常用於http請求中

#定義函式

def get_data(words):

data = {}

data["type"] = "auto"

data["i"] = words

data["doctype"] = "json"#資料解析成json格式

data["xmlversion"] = "1.8"

data["keyfrom:fanyi"] = "web"# 鑰匙來自於有道翻譯

data["ue"] = "utf-8"#資料解碼為'utf-8'

data["action"] = "fy_by_clickbutton"

data["typoresult"] = "true" #結果為真

data = urllib.parse.urlencode(data).encode('utf-8')#表單資料處理

return data #返回資料

def url_open(url, data):#開啟url,資料

req = urllib.request.request(url, data)#請求url,資料

response = urllib.request.urlopen(req)#響應請求url

html = response.read()#響應讀到的

html = html.decode("utf-8")#資料解碼為'utf-8'

return html #返回html

def get_json_data(html):#在json中得到資料

result = json.loads(html)#json載入

result = result['translateresult']#當有道能翻譯出該單詞,那麼那麼返回翻譯結果

result = result[0][0]['tgt']#當有道不能翻譯出該單詞,那麼會返回輸入單詞本身

return result

def main():

words = input("please input words: ")#輸入需要翻譯的中文或英文

# 有道詞典 api

url = ""#通過url請求有道位址

data = get_data(words)#得到在資料單詞

html = url_open(url, data)#發起請求

result = get_json_data(html)#得到結果

print("the result: %s" % result)#輸入結果

翻譯單詞彙總

ease n.容易 輕易 不費勁 舒適 安逸 自在 無憂無慮 v.使 寬慰 減輕 緩解 使 小心緩緩地移動 使 容易些 residual adj.剩餘的 殘留的 n.剩餘 殘渣 reformulate v.再制訂 再規劃 再準備 換種方式說 或表達 recognition n.認出 認識 識別 承認...

MySQL Workbench介面單詞翻譯

file new table new query tab open model open sql script open recent close tab exit 檔案 新建模型 新建查詢選項卡 開啟模型 開啟sql指令碼 開啟最近的 關閉選項卡 離開 edit undo redo cut cop...

leetcode 127 單詞接龍 python

給定兩個單詞 beginword 和 endword 和乙個字典,找到從 beginword 到 endword 的最短轉換序列的長度。轉換需遵循如下規則 每次轉換只能改變乙個字母。轉換過程中的中間單詞必須是字典中的單詞。說明 示例 1 輸入 beginword hit endword cog wo...