出自:http://blog.csdn.net/zhaoyl03/article/details/8830806

最近想动手做一个文档自动下载器,需要模拟浏览器的行为。虽然感觉思路上没有困难,但在技术细节上需要自己一步一步试探。在网上搜索相关内容的过程中,发现有人用Python调用Google翻译。我自己也试着实现这个小玩意,从而熟练和学习一些技术,如正则表达式匹配,模拟浏览器等。将这个小结果记录下来,以激励自己。

用Python调用Google翻译,就是模拟人将原文本(英语)粘贴在Google翻译的左边文本框,选择翻译设置从英文到简体中文,然后点击翻译,最后复制右边文本框中的翻译结果,并保存的过程。我比文献《用Python实现调用Google翻译》 的高明之处在,在提取翻译后的结果时,用正则表达式匹配很轻巧地抓取到了翻译后的文本。另外,代码完整。

我用的Pyhon版本2.66,源码如下:

 # -*- coding: utf-8 -*-
#Python -V: Python 2.6.6
#filename:GoogleTranslation1.2.py __author__ = "Yinlong Zhao (zhaoyl[at]sjtu[dot]edu[dot]cn)"
__date__ = "$Date: 2013/04/21 $" import re
import urllib,urllib2 #urllib:
#urllib2: The urllib2 module defines functions and classes which help in opening
#URLs (mostly HTTP) in a complex world — basic and digest authentication,
#redirections, cookies and more. def translate(text): '''模拟浏览器的行为,向Google Translate的主页发送数据,然后抓取翻译结果 ''' #text 输入要翻译的英文句子
text_1=text
#'langpair':'en'|'zh-CN'从英语到简体中文
values={'hl':'zh-CN','ie':'UTF-8','text':text_1,'langpair':"'en'|'zh-CN'"}
url='http://translate.google.cn/translate_t'
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
#模拟一个浏览器
browser='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
req.add_header('User-Agent',browser)
#向谷歌翻译发送请求
response = urllib2.urlopen(req)
#读取返回页面
html=response.read()
#从返回页面中过滤出翻译后的文本
#使用正则表达式匹配
#翻译后的文本是'TRANSLATED_TEXT='等号后面的内容
#.*? non-greedy or minimal fashion
#(?<=...)Matches if the current position in the string is preceded
#by a match for ... that ends at the current position
p=re.compile(r"(?<=TRANSLATED_TEXT=).*?;")
m=p.search(html)
text_2=m.group(0).strip(';')
return text_2 if __name__ == "__main__":
#text_1 原文
#text_1=open('c:\\text.txt','r').read()
text_1='Hello, my name is Derek. Nice to meet you! '
print('The input text: %s' % text_1)
text_2=translate(text_1).strip("'")
print('The output text: %s' % text_2) #保存结果
filename='c:\\Translation.txt'
fp=open(filename,'w')
fp.write(text_2)
fp.close() report='Master, I have done the work and saved the translation at '+filename+'.'
print('Report: %s' % report)

运行结果:

 >>>
The input text: Hello, my name is Derek. Nice to meet you!
The output text: 你好,我的名字是德里克。很高兴见到你!
Report: Master, I have done the work and saved the translation at c:\Translation.txt.
>>>

转载:http://blog.csdn.net/zhaoyl03/article/details/8830806

Python调用Google翻译的更多相关文章

  1. Python 调用百度翻译API

    由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...

  2. 调用google翻译

    1. [代码]maven依赖     ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency>     <groupId>org.a ...

  3. Java 调用 google 翻译

    1.Java代码 public class Translator { public String translate(String langFrom, String langTo, String wo ...

  4. node.js调用google翻译api

    源码下载:https://pan.baidu.com/s/1nxoodst 使用:(只支持get) http://39.106.33.56:3001/translate?text=Failure is ...

  5. 破解google翻译API全过程

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...

  6. 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展

    在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...

  7. Google翻译请求(难点是tk参数)

    业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...

  8. python 调用shell命令三种方法

    #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...

  9. 使用Python调用动态库

    我个人在日常使用电脑时,经常需要使用Google,于是就要切换代理,基本上是一会儿切换为代理,一会儿切换成直连,老是打开internet 选项去设置,很不方便,于是我萌生了一个想法: 做一个开关,我想 ...

随机推荐

  1. python 文件移动

    python实现文件移动: import shutil shutil.move("original_path", "new_folder") # move fi ...

  2. redis 查询key数量

    info查询所有库的key数量 dbsize查询当前库的key数量 keys * 查询当前库的所有key,数量少的时候可以用,当key很多的时候很慢,be careful!

  3. 获得Variant类型

    function GetVariantType(const v: variant): string; begin   case TVarData(v).vType of     varEmpty: r ...

  4. HTML CSS + DIV实现整体布局 part2

    9.盒模型的层次关系 我们通过一个经典的盒模型3D立体结构图来理解,如图:     从上往下看,层次关系如下: 第1层:盒子的边框(border),     第2层:元素的内容(content).内边 ...

  5. chrome innerHTML赋值

    例如: document.getElementById('id').innerHTML=data; 如果data的值是: "<div><tr><td>1& ...

  6. fiddler无法抓取chrome解决方法

    前端开发中,不可避免的要和服务器端进行联调,少了fiddler这个利器可不行. 由于无线开发需要配置UA,我使用chrome进行访问,但是今儿一早过来,发现fiddler无法抓取chrome的请求了. ...

  7. Ansible 从MySQL数据库添加或删除用户

    mysql_user - 从MySQL数据库添加或删除用户. 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 从MySQL数据库添加或删除用户. 要求(在执行模块的主机上) My ...

  8. OpenOffice Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  9. 87. Scramble String (String; DP)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  10. Django的模板语言介绍

    模板语言: 1.我们先看下在命令行中渲染模板 先导入模块对象 2.渲染一个变量 <p>当前时间:{{ time }}</p> return render(request,&qu ...