使用谷歌翻译翻译网站内容我发现错误“无法解码JSON对象”。这是代码:
import requests
from dragnet import extract_content
from googletrans import Translator
url = "https://bitcoin-bitcoin.themedia.jp/posts/4222377/"
r = requests.get(url)
content = extract_content(r.content)
translator = Translator()
text = translator.translate(content, dest='en').text
输出错误:
Traceback (most recent call last):
File "test.py", line 9, in <module>
text = translator.translate(content, dest='en').text
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 63, in _translate
data = utils.format_json(r.text)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
答案 0 :(得分:1)
根据错误,您传递的content
变量不符合预期格式。使用print(type(content))
和print(content)
了解您传递的信息以及是否为预期类型。
您使用的图书馆不是Google官方图书馆。找一个official example here。确实,您无法翻译整个网站,并且未实施等效的解决方案。你需要阅读页面并充分阅读;或者使用dragnet预处理content
。
在下面找到用于翻译API的Python客户端库示例:
# Imports the Google Cloud client library
from google.cloud import translate
# Instantiates a client
translate_client = translate.Client()
# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'
# Translates some text into Russian
translation = translate_client.translate(
text,
target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))