python實(shí)現(xiàn)在線翻譯
本文實(shí)例為大家分享了python實(shí)現(xiàn)在線翻譯的具體代碼,供大家參考,具體內(nèi)容如下
具體效果請(qǐng)看圖
代碼:
import urllib.requestimport urllib.parseimport jsondef translation(): while 1: print('-'*30) n = input('請(qǐng)選擇:1 翻譯 2 退出 :') if n ==’1’: content = input('請(qǐng)輸入要翻譯的內(nèi)容:') url = ’http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule’ data={} data[’i’]= content data[’from’]= ’AUTO’ data[’to’]=’AUTO’ data[’smartresult’] =’dict’ data[’client’]= ’fanyideskweb’ data[’salt’]= ’15790094838498’ data[’sign’]= ’9ab763875001c1949ae49d3c230ba19f’ data[’ts’]= ’1579009483849’ data[’bv’]= ’5a84f6fbcebd913f0a4e81b6ee54608’ data[’doctype’]= ’json’ data[’version’]= ’2.1’ data[’keyfrom’]= ’fanyi.web’ data[’action’]= ’FY_BY_CLICKBUTTION’ data = urllib.parse.urlencode(data).encode(’utf-8’) response = urllib.request.urlopen(url,data) html = response.read().decode(’utf-8’) #print(json.loads(html)) target =json.loads(html) print('翻譯結(jié)果;%s' % (target[’translateResult’][0][0][’tgt’])) elif n==’2’: print('感謝使用!') break else: print('輸入有誤!')if __name__==’__main__’: translation()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 寫(xiě)一個(gè)文件分發(fā)小程序2. Python本地及虛擬解釋器配置過(guò)程解析3. Python importlib模塊重載使用方法詳解4. Vue3中使用this的詳細(xì)教程5. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟6. Python中Anaconda3 安裝gdal庫(kù)的方法7. 用python對(duì)oracle進(jìn)行簡(jiǎn)單性能測(cè)試8. Python自動(dòng)化之定位方法大殺器xpath9. Python類(lèi)綁定方法及非綁定方法實(shí)例解析10. Python Selenium破解滑塊驗(yàn)證碼最新版(GEETEST95%以上通過(guò)率)
