当前位置: 移动技术网 > IT编程>脚本编程>Python > python翻译软件实现代码(使用google api完成)

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

2019年04月02日  | 移动技术网IT编程  | 我要评论

考生评语,布衣传说怎么样,嫩白

复制代码 代码如下:

# -*- 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 <- getline
    handle <- simplehttp (getrequest $ "http://translate.google.cn/translate_a/t?client=t&hl=zh-cn&sl=en&tl=zh-cn&ie=utf-8&oe=utf-8&" ++ (text word))
    content <- getresponsebody handle
    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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网