Python调用Google翻译
出自: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翻译的更多相关文章
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
- 调用google翻译
1. [代码]maven依赖 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency> <groupId>org.a ...
- Java 调用 google 翻译
1.Java代码 public class Translator { public String translate(String langFrom, String langTo, String wo ...
- node.js调用google翻译api
源码下载:https://pan.baidu.com/s/1nxoodst 使用:(只支持get) http://39.106.33.56:3001/translate?text=Failure is ...
- 破解google翻译API全过程
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...
- 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展
在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...
- Google翻译请求(难点是tk参数)
业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- 使用Python调用动态库
我个人在日常使用电脑时,经常需要使用Google,于是就要切换代理,基本上是一会儿切换为代理,一会儿切换成直连,老是打开internet 选项去设置,很不方便,于是我萌生了一个想法: 做一个开关,我想 ...
随机推荐
- sqlserver还原数据库
该方法只针对同等级数据库,不能跨级 比如sqlserver2012还原到sqlserver2008会报错 用数据库日志文件对数据库进行还原一 将日志文件.mdf文件和.ldf文件copy放置在sq ...
- Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To sta
有三种导致这种错误的原因. 第一个: 是因为tomcat的服务没有被关闭所导致的,将服务关闭即可 找到tomcat的安装目录,进入bin文件夹,找到tomcat7w.exe,双击这个文件,点击stop ...
- Kafka Manager 监控
1.安装: 依赖java环境,须首先安装java运行环境,并正确设置路径. 确保kafka已经安装,且版本合适. 修改配置文件: kafka-manager.zkhosts="你的zoo ...
- SQL Server:查看数据库用户权限(SQL 2005)
1. 查看 SQL 2005 用户所属数据库角色 use yourdb go select DbRole = g.name, MemberName = u.name, MemberSID = u.si ...
- HTML鼠标悬浮显示隐藏 JS方法
CSS样式表: @charset "utf-8"; /* CSS Document */ .a { width:80px; height:40px; top:200px; left ...
- etcd ui
https://github.com/henszey/etcd-browser docker build --build-arg http_proxy=http://109.105.4.17:3128 ...
- Rabbitmq 基本属性
MQ全称为Message Queue, 是一种分布式应用程序的的通信方法,它是消费-生产者模型的一个典型的代表,producer往消息队列中不断写入消息,而另一端consumer则可以读取或者订阅队列 ...
- Spring工作原理与单例
最近看到spring管理的bean为单例的,当它与web容器整合的时候始终搞不太清除,就网上搜索写资料, Tomcat与多线程, servlet是多线程执行的,多线程是容器提供的能力. servlet ...
- Error running : Address localhost:1099 is already in use
运行报错: Error running : Address localhost:1099 is already in use 解决方法: 打开任务管理器,将后台的java.exe进程都关掉,再次运行 ...
- Escape(状态压缩+最大流,好题)
Escape http://acm.hdu.edu.cn/showproblem.php?pid=3605 Time Limit: 4000/2000 MS (Java/Others) Memo ...