Copy codeThe Code is 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
Copy codeThe Code is 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 $ first match
Main
Text word = urlEncodeVars [("text", word)]
First: :( String, String, String)-> String
First (x, _, _) = x
Author: Hevienz