1.有道的翻譯
網(wǎng)頁: www.youdao.com

Fig1

Fig2

Fig3

Fig4
再次點(diǎn)擊"自動翻譯"->選中'Network'->選中'第一項(xiàng)',如下:

Fig5
然后顯示出如下內(nèi)容,紅框畫出的部分是等會編寫代碼需要的地方:

Fig6

Fig7
再看看翻譯的結(jié)果:

Fig8
2.python實(shí)現(xiàn)英譯漢:
原理:把需要翻譯的內(nèi)容輸入給有道詞典,然后通過程序把翻譯的結(jié)果爬下來。
1 # -*- coding:utf-8 -*- 2 """ 3 Created on Sun May 03 09:36:12 2015 4 5 @author: 90Zeng 6 """ 7 8 import urllib 9 import json10 11 # 注意這里用unicode編碼,否則會顯示亂碼12 content = input(u"請輸入要翻譯的內(nèi)容:")13 # 網(wǎng)址是Fig6中的 Response URL14 url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/'15 # 爬下來的數(shù)據(jù) data格式是Fig7中的 Form Data16 data = {}17 data['type'] = 'AUTO'18 data['i'] = content19 data['doctype'] = 'json'20 data['xmlVersion'] = '1.6'21 data['keyfrom'] = 'fanyi.web'22 data['ue'] = 'UTF-8'23 data['typoResult'] = 'true'24 25 # 數(shù)據(jù)編碼26 data = urllib.urlencode(data)27 28 # 按照data的格式從url爬內(nèi)容29 response = urllib.urlopen(url, data)30 # 將爬到的內(nèi)容讀出到變量字符串html,31 html = response.read()32 # 將字符串轉(zhuǎn)換成Fig8所示的字典形式33 target = json.loads(html)34 # 根據(jù)Fig8的格式,取出最終的翻譯結(jié)果35 result = target["translateResult"][0][0]['tgt']36 37 # 這里用unicode顯示中文,避免亂碼38 PRint(u"翻譯結(jié)果:%s" % (target["translateResult"][0][0]['tgt']))
運(yùn)行:


學(xué)習(xí)資料來源:小甲魚的視頻‘零基礎(chǔ)入門python’
新聞熱點(diǎn)
疑難解答