Python translation software implementation code (using google api)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.