2014-01-07 61 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

相关问题