You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用googletrans翻译印尼语推文遇TypeError:缺少text参数,求解决

Fixing the 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:08:54