2014-01-07 81 views
0

我試圖使用Tweepy python api創建一個twitter搜索流,但我面臨一個錯誤。這是我想執行的代碼,我得到的錯誤 -Tweepy Stream error:__init __()需要剛好3個參數(給出4個參數)

from tweepy.streaming import StreamListener 
from tweepy import OAuthHandler 
from tweepy import Stream 

# Twitter Credentials 
access_token_key = "*****" 
access_token_secret = "*****" 
consumer_key = "*****" 
consumer_secret = "*****" 

class StdOutListener(StreamListener): 
    """ A listener handles tweets that are the received from the stream. 
    This is a basic listener that just prints received tweets to stdout. 
    """ 

    def on_data(self, data): 
     print data 
     return True 

    def on_error(self, status): 
     print status 

if __name__ == '__main__': 
    x = StdOutListener() 
    auth = OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token_key, access_token_secret) 

    stream = Stream(auth, x, "microsoft") 
    response = stream.filter(track=['microsoft']) 

print response 
+0

你確定錯誤給你提供的方式,'_init_',而不是'__init__'? – alko

+0

它是雙下劃線....我懶得打字。抱歉! – dsauce

+1

@PreritAhuja:不要輸入錯誤信息;複製並粘貼它。 –

回答

0

問題出在流intitialization>

File "code.py", line 28, in <module> 
    stream = Stream(auth, x, "microsoft") 
__init__() takes exactly 3 arguments (4 given) 

(感謝您的幫助對不起,我是初學者。)行:

stream = Stream(auth, x, "microsoft") 

,應該是

stream = Stream(auth, x) 

跟蹤的單詞不通過構造函數傳遞,而是通過filter方法傳遞。

+0

我收到此錯誤----------文件「。\ twittersearch_v3.py」,第30行,在 response = stream.filter(track = ['microsoft']) File「build \ bdist .win32 \ egg \ tweepy \ streaming.py「,第242行,在過濾器 文件」build \ bdist.win32 \ egg \ tweepy \ streaming.py「,第181行,在_start 文件」build \ bdist.win32 \ egg \ tweepy \ streaming.py「,第120行,在_run ttributeError:'str'對象沒有屬性'on_error' – dsauce

+0

@PreritAhuja你會得到http 307還是AtrributeError?如果是後者,你可能會留下「微軟」而不是x。如果第一次,那麼你的tweepy可能太舊了,請檢查tweepy版本是否早於2.0 – alko

+0

這就是我正在使用的版本 x = StdOutListener() stream = Stream(auth,x) response = stream .filter(track = ['microsoft']) 打印回覆 } 剛剛變得「307」。它不會說別的,我正在使用tweepy 2.1 – dsauce

0

Stream()只需要2個參數(加self的綁定方法):

stream = Stream(auth, x) 
stream.filter(track=['microsoft']) 

見的Tweepy項目給出的streaming example

應該將307重定向處理爲非錯誤;你可以告訴Tweepy從on_error處理程序返回True繼續:

def on_error(self, status): 
    print status 
    if status == 307: 
     return True 
+0

嘗試了這一點,並獲得錯誤狀態307 – dsauce

相關問題