简体   繁体   English

我如何使用json google translate api?

[英]how do I use the json google translate api?

I am trying to use google translate from python with utf-8 text. 我正在尝试使用utf-8文本从python中使用谷歌翻译。 How do I call the json api? 我如何调用json api? They have a document for embedding it in html but I can't find a proper API or wsdl anywhere. 他们有一个用于在html中嵌入它的文档,但我无法在任何地方找到合适的API或wsdl。

Thanks Raphael 谢谢拉斐尔

Here is the code that finally works for me. 这是最终适合我的代码。 Using the website without the ajax api can get your ip banned, so this is better. 使用没有ajax api的网站可以禁止你的IP,所以这更好。

#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
import urllib2
import urllib
import simplejson
import sys

# The google translate API can be found here:
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
def translate(text = 'hola querida'):
    tl="es"
    sl="en"
    langpair='%s|%s'%(tl,sl)



    base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
    data = urllib.urlencode({'v':1.0,'ie': 'UTF8', 'q': text.encode('utf-8'),
                             'langpair':langpair})


    url = base_url+data

    search_results = urllib.urlopen(url)

    json = simplejson.loads(search_results.read())


    result = json['responseData']['translatedText']
    return result

Use xgoogle from Peteris Kramins ( His blog ) 使用Peteris Kramins的xgoogle( 他的博客

>>> from xgoogle.translate import Translator
>>>
>>> translate = Translator().translate
>>>
>>> print translate("Mani sauc Pēteris", lang_to="en")
My name is Peter
>>>
>>> print translate("Mani sauc Pēteris", lang_to="ru").encode('utf-8')
Меня зовут Петр
>>>
>>> print translate("Меня зовут Петр")
My name is Peter

Look what I have found : http://code.google.com/intl/ru/apis/ajaxlanguage/terms.html 看看我找到了什么: http//code.google.com/intl/ru/apis/ajaxlanguage/terms.html

Here is the interesting part: 这是有趣的部分:

You will not, and will not permit your end users or other third parties to: .... * submit any request exceeding 5000 characters in length; 您不会,也不会允许您的最终用户或其他第三方:...... *提交超过5000个字符的任何请求; .... ....

I think you are talking about the ajax api http://code.google.com/apis/ajaxlanguage/ , which has to be used from javascript, so I do not understand what do you mean by "google translate from python" 我想你在谈论ajax api http://code.google.com/apis/ajaxlanguage/ ,它必须在javascript中使用,所以我不明白你是什么意思“google translate from python”

Alternatively if you need to use translate functionality from python, you can directly query the translate page and parse it using xml/html libs eg beautiful soup, html5lib 或者,如果您需要使用python中的翻译功能,您可以直接查询翻译页面并使用xml / html库解析它,例如美丽的汤,html5lib

Actually I did that once and beautiful soup did not work on google translate but html5lib( http://code.google.com/p/html5lib/ ) did 实际上我做了一次,美丽的汤没有在谷歌翻译,但html5lib( http://code.google.com/p/html5lib/ )做了

you will need to do something like this (copied from my larger code base) 你将需要做这样的事情(从我更大的代码库复制)

def translate(text, tlan, slan="en"):

    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'translate.py/0.1')]

    htmlPage = opener.open(
            "http://translate.google.com/translate_t?" + 
            urllib.urlencode({'sl': slan, 'tl':tlan}),
            data=urllib.urlencode({'hl': 'en',
                                   'ie': 'UTF8',
                                   'text': text.encode('utf-8'),
                                   'sl': slan, 'tl': tlan})
        )

    parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("etree", cElementTree))

    etree_document = parser.parse(htmlPage)

    return _getResult(etree_document)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM