使用googletrans翻译网页内容时出现No JSON object could be decoded错误求助
Let's break down why you're hitting this error and walk through practical fixes:
What's Causing This?
That ValueError pops up because your version of googletrans is outdated and no longer matches Google's current translation API endpoints. On top of that, you're using Python 2.7—this version has been end-of-life for years, so almost all modern libraries (including newer googletrans releases) don't support it anymore.
Solutions to Try
1. Use a Python 2.7-Compatible googletrans Fork
Since the official googletrans dropped Python 2.7 support, install this maintained compatible version:
First, uninstall the old package:
pip uninstall googletrans
Then install the compatible test release:
pip install googletrans==3.1.0a0
Update your code to explicitly set the service URL (this fixes common connection mismatches):
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) # Explicitly define the service URL to avoid endpoint conflicts translator = Translator(service_urls=['translate.googleapis.com']) try: text = translator.translate(content, dest='en').text print(text) except Exception as e: print(f"Translation failed: {str(e)}")
Pro tip: Print content first to confirm extract_content is returning valid, non-empty text—garbled or empty input can also trigger weird API errors.
2. Migrate to Python 3 (Long-Term Fix)
Python 2.7 is no longer maintained, so switching to Python 3.8+ is the best long-term solution for stability. Once you make the switch:
Install the latest stable googletrans release:
pip install googletrans==4.0.0-rc1
Your code will work with minor Python 3 syntax adjustments:
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 print(text)
3. Use Google's Official Cloud Translation API (Most Stable)
If you need reliable translation for production, use Google's official API (you'll need a Google Cloud account and API key):
import requests from dragnet import extract_content API_KEY = "your-google-cloud-api-key" url = "https://bitcoin-bitcoin.themedia.jp/posts/4222377/" r = requests.get(url) content = extract_content(r.content) # Call official translation API translation_endpoint = f"https://translation.googleapis.com/language/translate/v2?key={API_KEY}" payload = { "q": content, "target": "en" } response = requests.post(translation_endpoint, json=payload) translation_result = response.json() text = translation_result['data']['translations'][0]['translatedText'] print(text)
Quick Pre-Test Checks
- Ensure your network can access Google's services (some regions may require a proxy)
- If your extracted content is very long, split it into smaller chunks—free translation endpoints have character limits
内容的提问来源于stack exchange,提问作者Federico Caccia

