2011-08-01 71 views
1

建議,請:)類型錯誤:__init __()至少需要4個非關鍵字參數(3給出)

當我使用這個腳本:

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) 

那我該怎麼辦?

+0

您使用的是什麼版本的Tweepy?它看起來像'tweepy.streaming.Stream'從[此提交]中取4個參數變爲3個參數(https://github.com/tweepy/tweepy/commit/13814fdf1f2dead020abc70257a94ddfadf0deb#tweepy/streaming.py)。 – icktoofay

回答

1

__init__是一個類的構造函數,在這種情況下是Stream。該錯誤意味着您向構造函數調用提供了錯誤數量的參數。

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()類作爲參數。查看是否可以在作爲參數傳遞給Stream()之前初始化CustomStreamListener()

0

這個問題的主要原因是使用老版本的tweepy。我在使用tweepy 1.7.1並且在更新tweepy 1.8之前發生了相同的錯誤。之後,問題解決了。我認爲應該接受4票贊成的答案來澄清解決方案。

相關問題