python3.2调用google翻译
2013-09-05 19:18
92 查看
原文使用python2.6:
修改在python3.2下测试成功
#coding=utf-8
__author__ = "zhangshy"
__date__ = "$Date: 2013/09/05$"
import re
import urllib.request
import urllib.parse
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.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url)
#模拟一个浏览器
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 = urllib.request.urlopen(req, data)
#读取返回页面
html = response.read().decode('utf-8')
# print (html)
#从返回页面中过滤出翻译后的文本
#使用正则表达式匹配
#翻译后的文本是'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
pattern = re.compile(r"(?<=TRANSLATED_TEXT=).*?;")
m = pattern.search(html)
text_2 = m.group(0).strip(';')
return text_2
if __name__ == "__main__":
text_1 = 'Hello. Nice to meet you'
print ('The input text: {0}'.format(text_1))
text_2 = translate(text_1).strip("'")
print ('The output text:{0}'.format(text_2))
修改在python3.2下测试成功
#coding=utf-8
__author__ = "zhangshy"
__date__ = "$Date: 2013/09/05$"
import re
import urllib.request
import urllib.parse
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.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url)
#模拟一个浏览器
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 = urllib.request.urlopen(req, data)
#读取返回页面
html = response.read().decode('utf-8')
# print (html)
#从返回页面中过滤出翻译后的文本
#使用正则表达式匹配
#翻译后的文本是'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
pattern = re.compile(r"(?<=TRANSLATED_TEXT=).*?;")
m = pattern.search(html)
text_2 = m.group(0).strip(';')
return text_2
if __name__ == "__main__":
text_1 = 'Hello. Nice to meet you'
print ('The input text: {0}'.format(text_1))
text_2 = translate(text_1).strip("'")
print ('The output text:{0}'.format(text_2))
相关文章推荐
- 小玩意系列:Python调用Google翻译
- python3.2 调用NLPIR(ICTLAS)2014的方法
- Python调用Google翻译
- python3调用Google翻译
- python3.2 调用NLPIR(ICTLAS)2014的方法
- python中调用lua——(lupa)
- python3.2 多线程小爬虫一只
- Python如何对MySQL存储过程进行调用
- Python3.2官方文档-日志和弱引用
- 获得CPU利用率(python调用top命令实现)
- 获得CPU利用率(python调用top命令实现)
- VS2010调用python编写的代码error:cannot open file 'python27_d.lib'".
- C++ 调用Python
- python 调用octave
- python调用父目录的模块
- python2.7 +matlabR2016b Python程序调用matlab程序
- python调用新浪微博API项目实践
- Selenium--调用js,对话框处理 (python)
- python写的一个omniORB的调用用例
- Python调用微博API