Python實現句子翻譯功能
初入Python,一開始就被她簡介的語法所吸引,代碼簡潔優(yōu)雅,之前在C#里面打開文件寫入文件等操作相比Python復雜多了,而Python打開、修改和保存文件顯得簡單得多。
1、打開文件的例子:
file=open('D:\\Python\\untitled\\Hello.txt','r',encoding='utf-8') data=file.read() print(data) file.close()
2、利用urllib庫請求頁面進行簡單的翻譯,請求百度翻譯,將要翻譯的內容當做參數傳給百度,然后將結果賦值給參數,最后打印出來:
上代碼:
import urllib.request import urllib.parse import json content=input("=====請輸入您要翻譯的內容:=====\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("翻譯結果為:%s"%(target['trans_result']['data'][0]['dst']))
實現效果如圖:
實現代碼很簡單,下面再分享下urllib庫的一些用法。
urlopen 語法
urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None) #url:訪問的網址 #data:額外的數據,如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()) # 超時設置 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')
響應
# 響應類型 import urllib.open response = urllib.request.urlopen('https:///www.python.org') print(type(response)) # 狀態(tài)碼, 響應頭 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' } # 構造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'))
總結
以上就是本文關于Python實現句子翻譯功能的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。
相關文章
使用python-Jenkins批量創(chuàng)建及修改jobs操作
這篇文章主要介紹了使用python-Jenkins批量創(chuàng)建及修改jobs操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python中的opencv和PIL(pillow)轉化操作
這篇文章主要介紹了python中的opencv和PIL(pillow)轉化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03