首页  >  文章  >  后端开发  >  python翻译软件实现代码(使用google api完成)

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

WBOY
WBOY原创
2016-06-16 08:46:101459浏览

复制代码 代码如下:

# -*- 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()

 haskell版

 

复制代码 代码如下:

 module Main where

import Network.HTTP
import Text.Regex.Posix

main = do
    putStrLn "Input the word you want to search:"
    word     handle     content     let match = (content =~ "\",\""::(String,String,String))
    putStrLn $ drop 4 $ first match
    main

text word = urlEncodeVars [("text",word)]

first::(String,String,String)->String
first (x,_,_) = x



作者:Hevienz
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn