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

代码如下:

# -*- coding: utf-8 -*- import httplibfrom urllib import urlencodeimport 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()

haskell版

代码如下:

module main where

import network.httpimport text.regex.posix

main = do putstrln “input the word you want to search:” word

发表评论