问题
Here is the code:
# coding: utf-8
from googletrans import Translator
translator = Translator()
print translator.translate('here', dest='zh-CN')
everything goes well before today. But not I try to use this python library. then I got this error.
root@localhost:~# python googletest.py
Traceback (most recent call last):
File "googletest.py", line 4, in <module>
print translator.translate(u'here', dest='zh-CN')
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 132, in translate
data = self._translate(text, dest, src)
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 57, in _translate
token = self.token_acquirer.do(text)
File "/usr/local/lib/python2.7/dist-packages/googletrans/gtoken.py", line 180, in do
self._update()
File "/usr/local/lib/python2.7/dist-packages/googletrans/gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
Does anyone know what is going on? thank you.
here is the library URL https://py-googletrans.readthedocs.io/en/latest/
回答1:
If you go deeper to library you see, that request to google translate site returns something like 503 error or other. For me it was 503 error and text:
This page appears when Google automatically detects requests coming from
your computer network which appear to be in violation of the Terms of Service.
The block will expire shortly after those requests stop.
This traffic may have been sent by malicious software, a browser plug-in,
or ascript that sends automated requests. If you share your network connection,
ask your administrator for help — a different computer using the same IP
address may be responsible. Learn more
Sometimes you may see this page if you are using advanced terms that robots
are known to use, or sending requests very quickly.
This means, and README of project says it:
If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
Try to use proxy.
来源:https://stackoverflow.com/questions/47725342/why-python-googletrans-suddenly-not-working