JSONDecodeError in Google Translate API

那年仲夏 提交于 2021-02-17 05:51:14

问题


I am trying to translate some data using googletrans python library. The below program first worked fine and after processing some requests it gives a JSONDecodeError. After some searching I found that Google may be blocking my IP address after a limited number of requests. Is this the actual problem? If then is there a solution? (Ex: using a vpn). Or else is there a fault in my code that causes the error?

My Code

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

Error

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)

One of the JSON files

[
  {
    "value": "Xiaomi",
    "synonyms": [
      "Xiaomi",
      "xiao mi",
      "MI",
      "mi"
    ]
  },
  {
    "value": "Android",
    "synonyms": [
      "Android",
      "android",
      "google os",
      "andriod"
    ]
  }
]

来源:https://stackoverflow.com/questions/55533634/jsondecodeerror-in-google-translate-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!