使用googletrans翻译印尼语推文遇TypeError:缺少text参数,求解决
TypeError: translate() missing 1 required positional argument: 'text' in googletrans Hey there! Let's break down why you're hitting this error and how to fix it quickly.
Why the Error Happens
The Translator class from googletrans requires you to create an instance of the class first before calling the translate() method. Right now, you're trying to call Translator.translate() directly on the class itself, which doesn't work—this method is designed to be used on an instance of the Translator, not the class blueprint.
Under the hood, when you call a method on a class instead of an instance, Python expects the first argument to be the instance (usually called self), but you're passing your tweet text as the first argument. That's why the error says it's missing the text parameter—Python thinks your tweet is the self argument and is looking for the actual text to translate next.
How to Fix It
You just need to create an instance of the Translator class first, then use that instance to call translate(). Here's the corrected code:
from googletrans import Translator # Create an instance of the Translator class translator = Translator() tweet = tweet.text # Save the tweet text tw_inggris = translator.translate(tweet, src='id', dest='en') # Optional: Access the translated text with .text attribute print(tw_inggris.text)
A quick note: Depending on the version of googletrans you're using, make sure you have a compatible version installed (some older versions might have slightly different syntax, but the instance-first approach is standard).
内容的提问来源于stack exchange,提问作者Berlian

