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

相关文章

在tensorflow中设置保存checkpoint的最大数量实例

 # Set up a RunConfig to only save checkpoints&...

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

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

python使用新浪微博api上传图片到微博示例

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

复制粘贴功能的Python程序

今天因为给BeauBeau提供的抽奖号码做SQL文件,一开始收到ZIP文件解开压缩之后被吓到了——29个CSV文件,每个文件保存了1000个奖券ID和号码-_-! 照上次一样,打开每个CS...

Python版的文曲星猜数字游戏代码

# -*- coding: utf-8 -*- import random #数字类 class NumberItem...