国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python利用有道翻譯實現"語言翻譯器"的功能實例

2020-02-16 10:41:08
字體:
來源:轉載
供稿:網友

實例如下:

import urllib.requestimport urllib.parseimport jsonwhile True:  content = input('請輸入需要翻譯的內容(退出輸入Q):')  if content == 'Q':    break  else:    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/'    data = {}    data['type'] = 'AUTO'    data['i'] = content    data['doctype'] = 'json'    data['xmlVersion'] = '1.8'    data['keyfrom'] = 'fanyi.web'    data['ue'] = 'UTF-8'    data['action'] = 'FY_BY_CLICKBUTTON'    data['typoResult'] = 'true'    data = urllib.parse.urlencode(data).encode('utf-8')    response = urllib.request.urlopen(url, data)    html = response.read().decode('utf-8')    target = json.loads(html)    print('翻譯的結果:%s' % target['translateResult'][0][0]['tgt'])

程序執行情況:

這里要注意的是兩個函數urllib.request.urlopen()與urllib.parse.urlencode()。

urllib.request.urlopen()其實不止一個參數,有好幾個哦,其中第二個是data,data應該是一個buffer的標準應用程序/ x-www-form-urlencoded格式(python標準庫原文:data should be a buffer in the standard application/x-www-form-urlencoded format)。urllib.parse.urlencode()函數接受一個映射或序列集合,并返回一個字符串的格式(python標準庫原文:The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format)。我們可以看看urllib.parse.urlencode()的結果是什么樣的:

上圖的結果剛好與urllib.request.urlopen()的data參數的數據類型要求一致了。

注意,上面urlopen當中的url,這個是分析有道翻譯頁面的真實的Request URL:

以上這篇python利用有道翻譯實現"語言翻譯器"的功能實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蛟河市| 微博| 九龙坡区| 廉江市| 于都县| 兖州市| 苍山县| 大化| 明水县| 永寿县| 茶陵县| 基隆市| 天祝| 金川县| 江陵县| 布拖县| 靖江市| 克山县| 阿坝| 东至县| 天门市| 崇明县| 贞丰县| 嘉定区| 福建省| 禹城市| 洪湖市| 乐平市| 三原县| 合阳县| 庆元县| 平阳县| 琼中| 奉新县| 九江县| 南木林县| 湖北省| 灵台县| 晋江市| 四平市| 延边|