Microsoft Translator仅对某些字符串提供HTTP错误400

时间:2014-06-05 12:51:31

标签: python urllib2 http-error microsoft-translator

我正在翻译大量字符串并使用urllib2向API发送请求。我的程序运行正常,但特别是在翻译某些字符串时,我总是得到HTTP Error 400。我做的每个请求都是完全相同的,除了text参数,所以我认为必须是文本以某种方式导致请求格式错误。以下是我知道的两个字符串,它总是会导致此错误:

  

@monaeltahawy hatla2eh fel jym bytmrn wla 3arf en fe 7aga bt7sl:d

  

@yaratambash ta3aly a3zemek 3l fetar

例如。

我确信它不是" @"导致错误的字符,或者" @"在字符串的前面。 API已经处理了具有这些属性的字符串。

这些字符串中的无意义词也不会导致问题,因为API之前也处理过无意义的单词。它只返回我发送给它的相同字符串。

以下是错误似乎来自的代码:

        tweet = tweet.encode("utf-8")
        to = "en" 
        translate_params = { 'text' : tweet, 'to' : to }
        request = urllib2.Request('http://api.microsofttranslator.com/v2/Http.svc/Translate?' + urllib.urlencode(translate_params))
        request.add_header('Authorization', 'Bearer '+ self.access_token)

        response = urllib2.urlopen(request)

        # Removes XML tags to return only the translated text
        response_text = ET.fromstring(response.read())
        response_text = ET.tostring(response_text, encoding = 'utf8', method = 'text')

        return response_text

我在Eclipse 4.3.2中运行Python 2.7。

非常感谢任何见解或建议。

0 个答案:

没有答案
相关问题