【问题标题】:TypeError: __init__() takes at least 4 non-keyword arguments (3 given)TypeError: __init__() 至少需要 4 个非关键字参数(给定 3 个)
【发布时间】:2015-02-01 14:03:19
【问题描述】:

请指教:)

当我使用这个脚本时:

class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):

        # We'll simply print some values in a tab-delimited format
        # suitable for capturing to a flat file but you could opt 
        # store them elsewhere, retweet select statuses, etc.



        try:
            print "%s\t%s\t%s\t%s" % (status.text, 
                                      status.author.screen_name, 
                                      status.created_at, 
                                      status.source,)
        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

# Create a streaming API and set a timeout value of 60 seconds.

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

# Optionally filter the statuses you want to track by providing a list
# of users to "follow".

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

streaming_api.filter(follow=None, track=Q)

出现这样的错误:

Traceback (most recent call last):
  File "C:/Python26/test.py", line 65, in <module>
    streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
TypeError: __init__() takes at least 4 non-keyword arguments (3 given)

那我该怎么办?

【问题讨论】:

  • 您使用的是什么版本的 Tweepy?看起来tweepy.streaming.Streamthis commit 中从采用 4 个参数变为 3 个参数。

标签: python api streaming arguments


【解决方案1】:

您的示例似乎来自here。您正在使用Tweepy,这是一个用于访问 Twitter API 的 Python 库。

来自 Github,hereStream() 对象的定义(假设您拥有最新版本的 Tweepy,请仔细检查!),

def __init__(self, auth, listener, **options):
        self.auth = auth
        self.listener = listener
        self.running = False
        self.timeout = options.get("timeout", 300.0)
        self.retry_count = options.get("retry_count")
        self.retry_time = options.get("retry_time", 10.0)
        self.snooze_time = options.get("snooze_time",  5.0)
        self.buffer_size = options.get("buffer_size",  1500)
        if options.get("secure"):
            self.scheme = "https"
        else:
            self.scheme = "http"

        self.api = API()
        self.headers = options.get("headers") or {}
        self.parameters = None
        self.body = None

因为您似乎传递了适当数量的参数,所以看起来CustomStreamListener() 没有被初始化,因此没有作为参数传递给Stream() 类。看看您是否可以在将 CustomStreamListener() 作为参数传递给 Stream() 之前对其进行初始化。

【讨论】:

    【解决方案2】:

    __init__ 是类的构造函数,在本例中为 Stream。该错误意味着您为构造函数调用提供了错误数量的参数。

    【讨论】:

      【解决方案3】:

      这个问题的主要原因是使用了旧版本的 tweepy。在更新 tweepy 1.8 之前,我使用的是 tweepy 1.7.1 并且遇到了同样的错误。紧接着,问题就解决了。我认为应该接受 4 票的答案以澄清解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多