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

时间:2021-05-22

复制代码 代码如下:
# -*- 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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章