谷歌翻译的一个可用网页接口

2017-04-10 13:45:47 +08:00
 zzcchh

谷歌翻译是比较好用的服务,可是大部分时间我们都上不去,在 stackflow 看到一个地址可以使用,在浏览器上输入 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q=Hello'返回了一个 f.txt 。 打开文件,翻译的结果就在一个很怪格式里面:[[["你好","Hello",null,null,1]],null,"en"]如果把 hello 替换成其他句子,返回的也是对应的结果。 sl=‘源语言’, tl=‘翻译语言’ 我的问题是,想利用这个‘接口’,用 python 做一个 txt 文件翻译小工具,但是以上的网页我用 urlopen 打不开,抛出 HTTPError: HTTP Error 403: Forbidden ,请问各位 v2er 利用什么模块获得返回的文本呢?如果来进行编写呢?

11261 次点击
所在节点    Python
10 条回复
jalena
2017-04-10 14:02:42 +08:00
应该是验证了 UA 的。。
JackyBao
2017-04-10 14:02:54 +08:00
国内可以直接打开
翻译服务 https://translate.google.cn
horsley
2017-04-10 14:04:49 +08:00
google cloud platform 上面有正经翻译 api 可用,收费
yantze
2017-04-10 15:40:25 +08:00
可以参考一下这个:
https://www.v2ex.com/t/301209
Kokororin
2017-04-10 15:43:48 +08:00
dsg001
2017-04-10 17:33:59 +08:00
>>> import requests
>>> url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q=Hello+world'
>>> r = requests.get(url)
>>> print(r.text)
[[["你好,世界","Hello world",null,null,1]],null,"en"]
>>>
cowpea
2017-04-11 09:48:22 +08:00
有道,扇贝都有 API ,扇贝还有读音
zzcchh
2017-04-11 10:21:49 +08:00
@cowpea 其实我们就是为了从围城里出去。
yunkchen
2017-04-11 11:31:54 +08:00
# -*- coding: UTF-8 -*-

"""
@author:yunkchen
@file:translate.py
@time:2017/4/10 0010 下午 1:52
"""
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
en2zh_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q="
zh2en_url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh&tl=en&dt=t&q="
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate, sdch, br",
"Accept-Language":"zh-CN,zh;q=0.8",
"User-Agent":"Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
"Referer": "http://wap.baidu.com"
}


def translate(word):
word = word.decode("utf-8")
if word >= u'\u4e00' and word<=u'\u9fa5':
response = requests.get(zh2en_url+word, timeout=60, headers=headers)
ch = response.content.split("\"")[1]
print(ch)
else:
response = requests.get(en2zh_url+word, timeout=60, headers=headers)
ch = response.content.split("\"")[1]
print(ch)

while True:
word = raw_input("Please input word:")
translate(word)
yucongo
2017-04-13 23:17:09 +08:00
requests 的 response 里其实有 json 数据!

不够钱帖源码, 参看 https://github.com/yucongo/mgoogle_translategoogleapis_translate.py ( Python 3.4 Win7 )

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/353781

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX