我试图使用Tweepy python api创建一个Twitter搜索流,但我面临一个错误。这是我要执行的代码以及我得到的错误 - >
File "code.py", line 28, in <module>
stream = Stream(auth, x, "microsoft")
__init__() takes exactly 3 arguments (4 given)
(感谢您的帮助。抱歉,我是初学者)
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 :(得分:0)
问题出在流初始化行:
stream = Stream(auth, x, "microsoft")
应该是
stream = Stream(auth, x)
跟踪的单词不是使用构造函数传递的,而是传递给filter
方法。
答案 1 :(得分:0)
Stream()
只需要2个参数(对于绑定方法加self
):
stream = Stream(auth, x)
stream.filter(track=['microsoft'])
参见Tweepy项目提供的streaming example。
307重定向应该被视为非错误;你可以告诉Tweepy继续从True
处理程序返回on_error
:
def on_error(self, status):
print status
if status == 307:
return True