手机
当前位置:查字典教程网 >脚本专栏 >python >python翻译软件实现代码(使用google api完成)
python翻译软件实现代码(使用google api完成)
摘要:复制代码代码如下:#-*-coding:utf-8-*-importhttplibfromurllibimporturlencodeimpo...

复制代码 代码如下:

# -*- 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翻译软件实现代码(使用google api完成)】相关文章:

通过python下载FTP上的文件夹的实现代码

Python天气预报采集器实现代码(网页爬虫)

python 查找文件夹下所有文件 实现代码

python的常见命令注入威胁

python学习笔记:字典的使用示例详解

python插入排序算法的实现代码

python实现猜数字游戏(无重复数字)示例分享

python登录QQ邮箱发信的实现代码

python中将阿拉伯数字转换成中文的实现代码

Python 文件操作实现代码

精品推荐
分类导航