当前位置:首页 > Python爬虫 > Python利用百度翻译API写翻译器

Python利用百度翻译API写翻译器

作者:二营长 发布时间:2016-12-11 点击:

Python用百度翻译API写个翻译器

#coding:utf-8     
_author_ = 'heziliang'    
import md5,random,requests,json,sys    
reload(sys)    
sys.setdefaultencoding("utf-8")    
def translate(q,lang):    
	appid = ''#自己申请的id    
	secretKey = '' #密钥    
	myurl = 'http://api.fanyi.baidu.com/api/trans/vip/translate'    
	fromLang = 'auto'    
	toLang = '%s' %lang    
	salt = random.randint(32768, 65536)    
	sign = appid+q+str(salt)+secretKey    
	m1 = md5.new()    
	m1.update(sign)    
	sign = m1.hexdigest()    
	#好像也不需要urllib.quote(q)    
	myurl = myurl+'?appid='+appid+'&q='+q+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign    
	r = requests.get(myurl)    
	jsondata = json.loads(r.content)    
	try:    
		print jsondata["trans_result"][0]["dst"]		    
	except Exception,e:    
		print e    
def chiToeng():    
	while 1:    
		print 'Please Input Chinses: '    
		chiWord = raw_input()    
		if(chiWord == '0'):    
			engTochi()    
		translate(chiWord.decode('gbk').encode('utf-8'),'en')    
		print '------------------------------------------------------------'    
		print 'Input 0 To Reselect language'    
def engTochi():    
	while 1:    
		print 'Please Input English: '     
		engWord = raw_input()    
		if(engWord == '0'):    
			chiToeng()    
		translate(engWord.decode('gbk').encode('utf-8'),'zh')    
		print '------------------------------------------------------------'    
		print 'Input 0 To Reselect language'    
def main():    
	print 'Chinses2English Input 1, English2Chinses Input 2,Change Language Input 0'    
	while 1:				    
		cOre = raw_input('Please Input:1 or 2: ')    
		if(cOre == '1'):				    
			chiToeng()    
		elif(cOre == '2'):			    
			engTochi()    
		elif(cOre == '0'):    
			pass    
		else:    
			print 'WTF!!!'    
main()


邮箱:techseo.cn@gmail.com,欢迎交流。
上一篇:Python爬虫浏览器useragent大全      下一篇:Python计数去重代码