TypeError:__ init __()至少需要4个非关键字参数(给定3个)

时间:2011-08-01 02:33:10

标签: python api streaming arguments

建议请:)

当我使用这个脚本时:

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)

那我该怎么办?

3 个答案:

答案 0 :(得分:4)

您的示例似乎来自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()

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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