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)類(lèi)型
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的Flask框架開(kāi)發(fā)環(huán)境的一些技巧總結(jié)
現(xiàn)在越來(lái)越多的人使用virtualenv虛擬環(huán)境部署Python項(xiàng)目,包括針對(duì)框架的實(shí)例文件夾與版本控制布置,這里我們就來(lái)整理關(guān)于深度定制Python的Flask框架開(kāi)發(fā)環(huán)境的一些技巧總結(jié)2016-07-07
python3+pyqt5+itchat微信定時(shí)發(fā)送消息的方法
今天小編就為大家分享一篇python3+pyqt5+itchat微信定時(shí)發(fā)送消息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python OpenCV機(jī)器學(xué)習(xí)之圖像識(shí)別詳解
OpenCV中也提供了一些機(jī)器學(xué)習(xí)的方法,例如DNN等。本文將為大家詳細(xì)介紹一下OpenCV中利用機(jī)器學(xué)習(xí)實(shí)現(xiàn)的一些圖片識(shí)別功能:人臉識(shí)別、車(chē)牌識(shí)別等,感興趣的可以了解一下2022-01-01
Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新
dwebsocket是Python中一款用于實(shí)現(xiàn)WebSocket協(xié)議的庫(kù),可用于后端數(shù)據(jù)實(shí)時(shí)刷新。在Django中結(jié)合使用dwebsocket和Channels,可以實(shí)現(xiàn)前后端的實(shí)時(shí)通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實(shí)時(shí)聊天、數(shù)據(jù)監(jiān)控、在線游戲等場(chǎng)景2023-04-04
pandas進(jìn)行數(shù)據(jù)輸入和輸出的方法詳解
這篇文章主要為大家詳細(xì)介紹了pandas進(jìn)行數(shù)據(jù)輸入和輸出的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
matplotlib基礎(chǔ)繪圖命令之imshow的使用
這篇文章主要介紹了matplotlib基礎(chǔ)繪圖命令之imshow的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹
這篇文章主要介紹了Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹,本文分別給出多個(gè)代碼實(shí)例來(lái)講解多種多樣的函數(shù)參數(shù),需要的朋友可以參考下2015-06-06

