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

yipeiwu_com5年前Python实例


# -*- 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实现代理服务功能实例

python实现代理服务功能实例

    代理服务原理很简单,就拿浏览器与web服务器来说。无非是A浏览器发request给B代理,B代理再把request把送给C web服务,然后C的reponse-&...

python wav模块获取采样率 采样点声道量化位数(实例代码)

安装:pip install wave在wav 模块中 ,主要介绍一种方法:getparams(),该方法返回的结果如下:_wave_params(nchannels=1, sa...

Python实现随机生成任意数量车牌号

Python实现随机生成任意数量车牌号

之前做课设的时候舍友遇到了需要生成500w量级车牌号的问题,于是我便写了一个随机生成车牌号的程序,希望各位采纳。注:Python实现import random def chepa...

使用Python实现Wake On Lan远程开机功能

Wake-On-LAN简称WOL,是一种电源管理功能;如果存在网络活动,则允许设备将操作系统从待机或休眠模式中唤醒。许多主板厂商支持IBM提出的网络唤醒标准。该标准允许网络管理员远程打开P...

浅谈tensorflow中Dataset图片的批量读取及维度的操作详解

三维的读取图片(w, h, c):import tensorflow as tf   import glob import ...