python翻译软件实现代码(使用google api完成)

727次阅读  |  发布于5年以前

复制代码 代码如下:

-- coding: utf-8 --

import httplib
from urllib import urlencode
import re

def out(text):
p = re.compile(r'","')
m = p.split(text)
print m[0][4:].decode('UTF-8').encode('GBK')

if name=='main':
while True:
word=raw_input('Input the word you want to search:')
text=urlencode({'text':word})
h=httplib.HTTP('translate.google.cn')
h.putrequest('GET', '/translate_a/t?client=t&hl;=zh-CN&sl;=en&tl;=zh-CN&ie;=UTF-8&oe;=UTF-8&'+text)
h.endheaders()
h.getreply()
f = h.getfile()
lines = f.readlines()
out(lines[0])
f.close()

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8