Copy Code code as follows:
#-*-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 Edition
Copy Code code as follows:
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 $ A match
Main
Text word = Urlencodevars [("Text", word)]
:(string,string,string)->string
A (x,_,_) = X
Author: Hevienz