POO*_*PTA 15 python http-error http-status-code-503 goslate
我刚开始使用Python中的goslate库来检测文本中单词的语言,但在测试了7-8个输入之后,我给出了输入,其中包含用阿拉伯语和英语两种语言编写的单词.之后,它开始给我错误.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
execfile("C:/test_goslate.py");
File "C:/test_goslate.py", line 12, in <module>
language_id = gs.detect('الدولة')
File "C:\Python27\lib\site-packages\goslate.py", line 484, in detect
return self._detect_language(text)
File "C:\Python27\lib\site-packages\goslate.py", line 448, in _detect_language
return self._basic_translate(text[:50].encode('utf-8'), 'en', 'auto')[1]
File "C:\Python27\lib\site-packages\goslate.py", line 251, in _basic_translate
response_content = self._open_url(url)
File "C:\Python27\lib\site-packages\goslate.py", line 181, in _open_url
response = self._opener.open(request, timeout=self._TIMEOUT)
File "C:\Python27\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 503: Service Unavailable
Run Code Online (Sandbox Code Playgroud)
我把代码编写为:
# -*- coding: utf8 -*-
import urllib2
import goslate
gs = goslate.Goslate()
language_id = gs.detect('wait ??????')
print (gs.get_languages()[language_id])
Run Code Online (Sandbox Code Playgroud)
现在它完全不能用于我之前测试过的任何输入并且给我同样的错误.我尝试在谷歌上找到错误解决但没有任何帮助.这是我发现的: 链接1 - StackOverflow
我尝试使用上面的链接中建议的命令更新它:
pip install -U goslate
Run Code Online (Sandbox Code Playgroud)
但它没有帮助,因为它已经是我正在使用的最新版本.另外,我在图书馆文档中读到,在以下情况下会出现这种错误的翻译:
If you get HTTP 5xx error, it is probably because google has banned your client IP address from transation querying.
You could verify it by access google translation service in browser manually.
You could try the following to overcome this issue:
query through a HTTP/SOCK5 proxy, see Proxy Support
using another google domain for translation: gs = Goslate(service_urls=['http://translate.google.de'])
wait for 3 seconds before issue another querying
Run Code Online (Sandbox Code Playgroud)
我尝试使用代理连接,但没有任何帮助.
编辑 原因可能是Google每天只允许一些请求吗?在那种情况下,可以做得更好吗?有没有其他基于Python的库可以帮我解决这个问题?
pro*_*r44 11
也许正在寻找这个:https://pypi.python.org/pypi/textblob它比goslate更好,
因为textblob截止到现在,也许py-translate可以做到这一点,
https://pypi.python.org/pypi/py-translate/#downloads
http://pythonhosted.org/py-translate/devs/api.html
from translate import translator
translator('en', 'es', 'Hello World!')
Run Code Online (Sandbox Code Playgroud)
"py-translate是用Python编写的谷歌翻译的CLI工具!"
翻译函数的第一个参数是源语言,第二个是目标语言,第三个是要翻译的短语,
它返回一个字典,文档将其称为请求接口
详细解释@ programmer44的答案,这是一个使用TextBlob来解决这个特殊情况的例子:
from textblob.blob import TextBlob
blob = TextBlob('wait ??????')
print(blob.detect_language())
Run Code Online (Sandbox Code Playgroud)