Python實(shí)現(xiàn)句子翻譯功能
初入Python,一開(kāi)始就被她簡(jiǎn)介的語(yǔ)法所吸引,代碼簡(jiǎn)潔優(yōu)雅,之前在C#里面打開(kāi)文件寫(xiě)入文件等操作相比Python復(fù)雜多了,而Python打開(kāi)、修改和保存文件顯得簡(jiǎn)單得多。
1、打開(kāi)文件的例子:
file=open('D:\\Python\\untitled\\Hello.txt','r',encoding='utf-8') data=file.read() print(data) file.close()
2、利用urllib庫(kù)請(qǐng)求頁(yè)面進(jìn)行簡(jiǎn)單的翻譯,請(qǐng)求百度翻譯,將要翻譯的內(nèi)容當(dāng)做參數(shù)傳給百度,然后將結(jié)果賦值給參數(shù),最后打印出來(lái):
上代碼:
import urllib.request import urllib.parse import json content=input("=====請(qǐng)輸入您要翻譯的內(nèi)容:=====\n") url='http://fanyi.baidu.com/v2transapi' data={} data['from']='zh' data['to']='en' data['transtype']='translang' data['simple_means_flag']='3' data['query']=content 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("翻譯結(jié)果為:%s"%(target['trans_result']['data'][0]['dst']))
實(shí)現(xiàn)效果如圖:
實(shí)現(xiàn)代碼很簡(jiǎn)單,下面再分享下urllib庫(kù)的一些用法。
urlopen 語(yǔ)法
urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None) #url:訪問(wèn)的網(wǎng)址 #data:額外的數(shù)據(jù),如header,form data
用法
# request:GET import urllib.request response = urllib.request.urlopen('http://www.baidu.com') print(response.read().decode('utf-8')) # request: POST # http測(cè)試:http://httpbin.org/ import urllib.parse import urllib.request data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8') response = urllib.request.urlopen('http://httpbin.org/post',data=data) print(response.read()) # 超時(shí)設(shè)置 import urllib.request response = urllib.request.urlopen('http://httpbin.org/get',timeout=1) print(response.read()) import socket import urllib.request import urllib.error try: response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1) except urllib.error.URLError as e: if isinstance(e.reason,socket.timeout): print('TIME OUT')
響應(yīng)
# 響應(yīng)類型 import urllib.open response = urllib.request.urlopen('https:///www.python.org') print(type(response)) # 狀態(tài)碼, 響應(yīng)頭 import urllib.request response = urllib.request.urlopen('https://www.python.org') print(response.status) print(response.getheaders()) print(response.getheader('Server'))
Request
聲明一個(gè)request對(duì)象,該對(duì)象可以包括header等信息,然后用urlopen打開(kāi)。
# 簡(jiǎn)單例子 import urllib.request request = urllib.request.Requests('https://python.org') response = urllib.request.urlopen(request) print(response.read().decode('utf-8')) # 增加header from urllib import request, parse url = 'http://httpbin.org/post' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' 'Host':'httpbin.org' } # 構(gòu)造POST表格 dict = { 'name':'Germey' } data = bytes(parse.urlencode(dict),encoding='utf8') req = request.Request(url=url,data=data,headers=headers,method='POST') response = request.urlopen(req) print(response.read()).decode('utf-8') # 或者隨后增加header from urllib import request, parse url = 'http://httpbin.org/post' dict = { 'name':'Germey' } req = request.Request(url=url,data=data,method='POST') req.add_hader('User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36') response = request.urlopen(req) print(response.read().decode('utf-8'))
總結(jié)
以上就是本文關(guān)于Python實(shí)現(xiàn)句子翻譯功能的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
python+opencv實(shí)現(xiàn)的簡(jiǎn)單人臉識(shí)別代碼示例
python實(shí)現(xiàn)圖片處理和特征提取詳解
如有不足之處,歡迎留言指出。
相關(guān)文章
python判斷兩個(gè)序列的成員是否一樣的實(shí)例代碼
在本篇文章里小編給大家整理了關(guān)于python判斷兩個(gè)序列的成員是否一樣的實(shí)例代碼,需要的朋友們參考下。2020-03-03[機(jī)器視覺(jué)]使用python自動(dòng)識(shí)別驗(yàn)證碼詳解
這篇文章主要介紹了python自動(dòng)識(shí)別驗(yàn)證碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05使用Python FastAPI構(gòu)建Web服務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了使用Python FastAPI構(gòu)建Web服務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python讀取xlsx數(shù)據(jù)生成圖標(biāo)代碼實(shí)例
這篇文章主要介紹了Python讀取xlsx數(shù)據(jù)生成圖標(biāo)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08使用python-Jenkins批量創(chuàng)建及修改jobs操作
這篇文章主要介紹了使用python-Jenkins批量創(chuàng)建及修改jobs操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05python中的opencv和PIL(pillow)轉(zhuǎn)化操作
這篇文章主要介紹了python中的opencv和PIL(pillow)轉(zhuǎn)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Python中map和列表推導(dǎo)效率比較實(shí)例分析
這篇文章主要介紹了Python中map和列表推導(dǎo)效率比較,實(shí)例分析了Python中的map與列表的推導(dǎo)效率,需要的朋友可以參考下2015-06-06