2

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

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

@yaratambash ta3aly a3zemek 3l 胎儿

例如。

我确定不是“@”字符导致错误,或者“@”位于字符串的前面。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。

任何见解或建议将不胜感激。

4

0 回答 0