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

yipeiwu_com6年前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使用新浪微博api上传图片到微博示例

import urllib.parse,os.path,time,sys from http.client import HTTPSConnection f...

python实现代理服务功能实例

python实现代理服务功能实例

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

FFmpeg提取视频音频python将音频转文字

ffmpeg提取视频中的音频-pcmffmpeg -y -i input.mp4 -vn -codec copy out.m4a...

python发送邮件的实例代码(支持html、图片、附件)

第一段代码:#!/usr/bin/python # -*- coding: utf-8 -*- import email imp...

python动态文本进度条的实例代码

python动态文本进度条的实例代码

如何实现动态单行刷新,答案是——覆盖但是怎么实现覆盖呢关键在于不换行而且能回退到开始位置那么就要用到 \r这个东西就是让光标回退到当前行初始位置记得不能让换行上码#文本进度条.p...