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


Posted in Python onNovember 26, 2013
# -*- 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使用urllib模块的urlopen超时问题解决方法
Nov 08 Python
详解Python中的序列化与反序列化的使用
Jun 30 Python
python实现装饰器、描述符
Feb 28 Python
Python实现将数据框数据写入mongodb及mysql数据库的方法
Apr 02 Python
Python根据文件名批量转移图片的方法
Oct 21 Python
使用python获取电脑的磁盘信息方法
Nov 01 Python
详解Python基础random模块随机数的生成
Mar 23 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
python中bytes和str类型的区别
Oct 21 Python
python函数enumerate,operator和Counter使用技巧实例小结
Feb 22 Python
基于django2.2连oracle11g解决版本冲突的问题
Jul 02 Python
Python运算符+与+=的方法实例
Feb 18 Python
python高并发异步服务器核心库forkcore使用方法
Nov 26 #Python
跨平台python异步回调机制实现和使用方法
Nov 26 #Python
python连接池实现示例程序
Nov 26 #Python
使用python BeautifulSoup库抓取58手机维修信息
Nov 21 #Python
python BeautifulSoup使用方法详解
Nov 21 #Python
python装饰器使用方法实例
Nov 21 #Python
tornado框架blog模块分析与使用
Nov 21 #Python
You might like
简单的PHP缓存设计实现代码
2011/09/30 PHP
PHP 文件系统详解
2012/09/13 PHP
解析php获取字符串的编码格式的方法(函数)
2013/06/21 PHP
php一维二维数组键排序方法实例总结
2014/11/13 PHP
PHP针对伪静态的注入总结【附asp与Python相关代码】
2017/08/01 PHP
在PHP中实现使用Guzzle执行POST和GET请求
2019/10/15 PHP
Firebug入门指南(Firefox浏览器)
2010/08/21 Javascript
capacityFixed 基于jquery的类似于新浪微博新消息提示的定位框
2011/05/24 Javascript
Javascript面向对象编程(二) 构造函数的继承
2011/08/28 Javascript
jquery五角星评分插件示例分享
2014/02/21 Javascript
JavaScript实现将数组数据添加到Select下拉框的方法
2015/08/21 Javascript
JS+CSS实现滑动切换tab菜单效果
2015/08/25 Javascript
jQuery中 $ 符号的冲突问题及解决方案
2016/11/04 Javascript
vue 如何添加全局函数或全局变量以及单页面的title设置总结
2017/06/01 Javascript
浅谈Vue.use的使用
2018/08/29 Javascript
详解Jest结合Vue-test-utils使用的初步实践
2019/06/27 Javascript
Json实现传值到后台代码实例
2020/06/30 Javascript
基于postman获取动态数据过程详解
2020/09/08 Javascript
OpenLayers加载缩放控件使用方法详解
2020/09/25 Javascript
初学python的操作难点总结(新手必看篇)
2017/08/03 Python
使用Python爬取最好大学网大学排名
2018/02/24 Python
Python 查找字符在字符串中的位置实例
2018/05/02 Python
Django使用中间键实现csrf认证详解
2019/07/22 Python
Python re 模块findall() 函数返回值展现方式解析
2019/08/09 Python
pygame实现打字游戏
2021/02/19 Python
Python多进程编程常用方法解析
2020/03/26 Python
Django admin管理工具TabularInline类用法详解
2020/05/14 Python
PyTorch预训练Bert模型的示例
2020/11/17 Python
Boom手表官网:瑞典手表品牌,设计你的手表
2019/03/11 全球购物
幼儿园教师教学反思
2014/02/06 职场文书
2014年创卫实施方案
2014/02/18 职场文书
合作意向书格式及范文
2014/03/31 职场文书
助人为乐模范事迹材料
2014/06/02 职场文书
生产车间标语
2014/06/11 职场文书
社会主义核心价值观主题教育活动总结
2015/05/07 职场文书
新员工入职感言范文!
2019/07/04 职场文书