亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年11月14日 15:36:05   作者:HelloJquery  
這篇文章主要介紹了Python實現(xiàn)句子翻譯功能,涉及urllib庫的使用等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。

初入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.request
import urllib.parse
import json

content=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']=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']))

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

實現(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:GET
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))

# request: POST
# http測試: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è)置
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

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

# 簡單例子
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實現(xiàn)句子翻譯功能的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

python+opencv實現(xiàn)的簡單人臉識別代碼示例

Python編程實現(xiàn)蟻群算法詳解

python實現(xiàn)圖片處理和特征提取詳解

如有不足之處,歡迎留言指出。

相關(guān)文章

最新評論