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

2022-01-21 00:00:00 python api arguments streaming

问题描述

请指教:)

当我使用这个脚本时:

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	%s	%s	%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,这是一个用于访问 Twitter API 的 Python 库.

Your example appears to be from here. And you are using Tweepy, a Python library for accessing the Twitter API.

来自 Github,这里是一个Stream() 对象(假设你有最新版本的 Tweepy,请仔细检查!),

From Github, here is the definition of a Stream() object (assuming you have the latest version of Tweepy, please double check!),

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() 之前对其进行初始化.

Because you seemed to have passed in the appropriate number of arguments, it looks like CustomStreamListener() isn't being initialized, and therefore isn't being passed to the Stream() class as an argument. See if you can initialize a CustomStreamListener() prior to being passed as an argument to Stream().

相关文章