import urllib.request
import urllib.parse
import json


content = input('請輸入要翻譯內容:')


# url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'        #設定前往的網路地址


data = {}                         #建立字典data   來取代form data
#字典data裡面新增鍵值對
data['i'] = content            #我們輸入的要翻譯的內容
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1538036769641'
data['sign'] = '55fee779866d4a78185be7d2bf67a40a'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data['typoResult'] = 'false'

data = urllib.parse.urlencode(data).encode('utf-8')            #字典資料編碼

req = urllib.request.Request(url,data)                 #建立request請求,請求裡面包括各種資訊
#新增request的header裡面的User-Agent
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')


response = urllib.request.urlopen(req)                #傳送request請求,接收response反應
html = response.read().decode('utf-8')               #返回的內容解碼
print(html)
# print(type(html))
html = json.loads(html)                           #將json資料轉為字串形式
print(html)
html = html['translateResult'][0][0]['tgt']      #鎖定 key:tgt  ,並展示它的值value
print(html)

這裡的字典data用來偽造瀏覽器post請求攜帶的form data。
form data這麼看:滑鼠在‘’翻譯‘’上面點選右鍵,進入審查元素或者檢查,裡面的network裡面,此時輸入翻譯的英文,network裡面有一個translate檔案,點選右邊會出現介紹,裡面有個form data。