【问题标题】:Python - google-trans-new throwing error on code that was workingPython - google-trans-new 对正在运行的代码抛出错误
【发布时间】:2021-09-17 01:19:00
【问题描述】:

我一直在使用 Google Translate API 自动翻译几个单词,这几个月来一直运行良好,但 2-3 天前我开始遇到问题,因为这个 sn-p 正在提高一个错误。

代码

from google_trans_new import google_translator

translator = google_translator()
print(status) #light rain 
translate_text = translator.translate(status, lang_tgt='pt')
print(translate_text) #chuva leve

错误

File "bot.py", line 74, in checkBadConditions
    translate_text = translator.translate(status, lang_tgt='pt')
  File "/home/pi/.local/lib/python3.7/site-packages/google_trans_new/google_trans_new.py", line 188, in translate
    raise e
  File "/home/pi/.local/lib/python3.7/site-packages/google_trans_new/google_trans_new.py", line 152, in translate
    response = json.loads(response)
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 358 (char 357)

我也试过translate_text = translator.translate(status, lang_src='en', lang_tgt='pt'),也没用

有更新吗?我做错了什么

【问题讨论】:

  • 您提供的状态是否与以前的格式不同?输入数据看起来确实不像预期的那样
  • 状态不是问题,因为我已将状态切换到 "Hello" 并且出现错误
  • 我在我的机器上试过了,同样的错误。从official github,我们可以看到他们在 6 天前添加了错误修复。它是否像 4 天前一样工作,或者错误修复可能是出现此错误的原因?还要检查 10 天前的 this issue,这似乎与您的错误非常相关

标签: python google-translate google-translation-api


【解决方案1】:

这似乎是来自包google-trans-new 的错误,这是已知的并且已经更正。 (查看this讨论以获取更多信息)。

带有错误修复的新版本模块尚未发布到 pip。所以必须手动修改或者等待newt版本发布。

【讨论】:

    【解决方案2】:
    from bs4 import BeautifulSoup
    from bs4.formatter import HTMLFormatter
    from googletrans import Translator
    import requests
    
    translator = Translator()
    
    class UnsortedAttributes(HTMLFormatter):
        def attributes(self, tag):
            for k, v in tag.attrs.items():
                yield k, v
    

    检查这 2 个 Python 转换器代码的替代方案:

    https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-library.html

    或这里:

    https://neculaifantanaru.com/en/example-google-translate-api-key-python-code-beautifulsoup.html

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 2021-08-18
      • 2018-08-05
      • 1970-01-01
      相关资源
      最近更新 更多