python翻译软件实现代码(使用google api完成)
# -*- 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
相关文章
python django事务transaction源码分析详解
这篇文章主要介绍了python django事务transaction源码分析详解的相关资料,需要的朋友可以参考下2017-03-03python神经网络tfrecords文件的写入读取及内容解析
这篇文章主要为大家介绍了python神经网络tfrecords文件的写入读取及内容解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-05-05
最新评论