yycGIS

我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容。 我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系。

Less is more, more is different!

导航

Python调用Google翻译

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

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

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

 

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

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

运行结果:

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

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

posted on 2014-04-14 14:47  yycGIS  阅读(1969)  评论(1编辑  收藏  举报