出自: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. 【338】Pandas.DataFrame

    Ref: Pandas Tutorial: DataFrames in Python Ref: pandas.DataFrame Ref: Pandas:DataFrame对象的基础操作 Ref: C ...

  2. express + mongodb 搭建一个简易网站 (三)

    express + mongodb 搭建一个简易网站 (三) 前面已经实现了基本的网站功能,现在我们就开始开搞一个完整的网站,现在整个网站的UI就是下面的这个样子. 我们网站的样子就照着这个来吧. 1 ...

  3. 常用数据库ID格式

    转自:http://www.biotrainee.com/thread-411-1-1.html 常用数据库 ID ID 示例 ID 来源 ENSG00000116717 Ensemble ID GA ...

  4. Spring boot集成 MyBatis 通用Mapper

    配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  5. mysql for update语句

    我们都知道for update语句会锁住一张表,锁表的细节很多人却不太清楚,下面我们举例看下. 在表上我们有个索引,如下: 现在在我们通过索引store_id锁表: 我们再开一个客户端,还是锁住同一个 ...

  6. Java连接MySQL数据库及操作

    Java操作MySQL数据库,需要驱动mysql-connector-java 来进行操作,去下载对应的jar包   一.导入需要的jar包 我用的是maven对包进行管理,在maven中添加如下内容 ...

  7. java rsa 加解密

    参考 http://blog.csdn.net/a394268045/article/details/52232120 package rsa; import org.apache.commons.c ...

  8. hdoj1069 Monkey and Banana(DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 思路: 由题意,显然一种block可能有6种形式,且一种形式最多使用一次,因此最多有30×6=1 ...

  9. Intersection of Two Linked Lists(LIST-2 POINTER)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  10. MongoDB的数据类型(四)

    JSON JSON是一种简单的数据表示方式,它易于理解.易于解析.易于记忆.但从另一方面来说,因为只有null.布尔.数字.字符串.数组和对象这几种数据类型,所以JSON有一定局限性.例如,JSON没 ...