我正在尝试使用googletrans python库转换某些数据。下面的程序首先运行良好,并且在处理了一些请求之后给出了JSONDecodeError
。经过一番搜索,我发现Google在请求数量有限后可能会阻止我的IP地址。这是实际问题吗?如果可以的话,有解决办法吗? (例如:使用vpn)。否则我的代码中存在导致错误的错误?
我的代码
import glob
import errno
import json
from googletrans import Translator
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.knowledge
en_entities = db.en_entities
ta_entities = db.ta_entities
translator = Translator()
path = '../data/DialogFlow Data/entities/*entries*.json'
files = glob.glob(path)
for name in files:
try:
with open(name) as f:
entities = json.load(f)
for entity in entities:
synonyms = []
translations = translator.translate(entity['synonyms'], dest='ta')
for translation in translations:
synonyms.append(translation.text)
dic = {'value': entity['value'], 'synonyms': synonyms}
ta_entities.insert_one(dic)
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
错误
Traceback (most recent call last):
File "/snap/pycharm-professional/125/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/snap/pycharm-professional/125/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/snap/pycharm-professional/125/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/snap/pycharm-professional/125/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/kabilesh/PycharmProjects/Chatbot/entity_handlers/put_entities.py", line 38, in <module>
translations = translator.translate(entity['synonyms'], dest='ta')
File "/home/kabilesh/PycharmProjects/Chatbot/venv/lib/python3.6/site-packages/googletrans/client.py", line 167, in translate
translated = self.translate(item, dest=dest, src=src)
File "/home/kabilesh/PycharmProjects/Chatbot/venv/lib/python3.6/site-packages/googletrans/client.py", line 172, in translate
data = self._translate(text, dest, src)
File "/home/kabilesh/PycharmProjects/Chatbot/venv/lib/python3.6/site-packages/googletrans/client.py", line 81, in _translate
data = utils.format_json(r.text)
File "/home/kabilesh/PycharmProjects/Chatbot/venv/lib/python3.6/site-packages/googletrans/utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "/home/kabilesh/PycharmProjects/Chatbot/venv/lib/python3.6/site-packages/googletrans/utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
其中一个JSON文件
[
{
"value": "Xiaomi",
"synonyms": [
"Xiaomi",
"xiao mi",
"MI",
"mi"
]
},
{
"value": "Android",
"synonyms": [
"Android",
"android",
"google os",
"andriod"
]
}
]