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

首頁 > 編程 > Python > 正文

Python實現(xiàn)句子翻譯功能

2020-01-04 16:23:08
字體:
供稿:網(wǎng)友

初入Python,一開始就被她簡介的語法所吸引,代碼簡潔優(yōu)雅,之前在C#里面打開文件寫入文件等操作相比Python復(fù)雜多了,而Python打開、修改和保存文件顯得簡單得多。

1、打開文件的例子:

 file=open('D://Python//untitled//Hello.txt','r',encoding='utf-8') data=file.read() print(data) file.close()

2、利用urllib庫請求頁面進行簡單的翻譯,請求百度翻譯,將要翻譯的內(nèi)容當做參數(shù)傳給百度,然后將結(jié)果賦值給參數(shù),最后打印出來:

上代碼:

import urllib.requestimport urllib.parseimport jsoncontent=input("=====請輸入您要翻譯的內(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']=contentdata=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']))

實現(xiàn)效果如圖:

python,urllib庫

實現(xiàn)代碼很簡單,下面再分享下urllib庫的一些用法。

urlopen 語法

urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None)#url:訪問的網(wǎng)址#data:額外的數(shù)據(jù),如header,form data

用法

# request:GETimport urllib.requestresponse = urllib.request.urlopen('http://www.baidu.com')print(response.read().decode('utf-8'))# request: POST# http測試:http://httpbin.org/import urllib.parseimport urllib.requestdata = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')response = urllib.request.urlopen('http://httpbin.org/post',data=data)print(response.read())# 超時設(shè)置import urllib.requestresponse = urllib.request.urlopen('http://httpbin.org/get',timeout=1)print(response.read())import socketimport urllib.requestimport urllib.errortry:  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.openresponse = urllib.request.urlopen('https:///www.python.org')print(type(response))# 狀態(tài)碼, 響應(yīng)頭import urllib.requestresponse = urllib.request.urlopen('https://www.python.org')print(response.status)print(response.getheaders())print(response.getheader('Server'))

Request

聲明一個request對象,該對象可以包括header等信息,然后用urlopen打開。

# 簡單例子import urllib.requestrequest = urllib.request.Requests('https://python.org')response = urllib.request.urlopen(request)print(response.read().decode('utf-8'))# 增加headerfrom urllib import request, parseurl = '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')# 或者隨后增加headerfrom urllib import request, parseurl = '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實現(xiàn)句子翻譯功能的全部內(nèi)容,希望對大家有所幫助。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 游戏| 什邡市| 启东市| 杨浦区| 措美县| 晴隆县| 广水市| 庆阳市| 五河县| 云林县| 遵化市| 邹城市| 永靖县| 德惠市| 民和| 多伦县| 灵台县| 乌鲁木齐县| 阿克陶县| 甘德县| 灌阳县| 谷城县| 卢氏县| 和田县| 长垣县| 迁西县| 洪泽县| 明光市| 揭阳市| 册亨县| 六枝特区| 蚌埠市| 陈巴尔虎旗| 博湖县| 安康市| 乳源| 新绛县| 新源县| 衡东县| 西昌市| 临潭县|