我有以下简单的代码片段之前工作正常,但现在不是:
import sys
import tweepy
# Consumer keys and access tokens, used for OAuth
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print >>status.text
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
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(locations=[-180,-90,180,90])
此代码应打印来自世界各地的推文。但是,我收到错误:
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
TypeError: __init__() missing 1 required positional argument: 'listener'
请帮忙谢谢!
编辑:
所以我改变了
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
为:
sapi = tweepy.streaming.Stream(auth, listener=CustomStreamListener())
现在我得到以下(类似)错误:
sapi = tweepy.streaming.Stream(auth, listener=CustomStreamListener())
TypeError: __init__() missing 1 required positional argument: 'password'
也许这会让人知道这里发生了什么,因为我不知道。
由于
答案 0 :(得分:0)
我认为问题在于您使用的是过时的Tweepy版本。您的代码是正确的,但是Tweepy的旧版本对Stream
类有不同的构造函数(它需要传递用户名和密码而不是OAuthHandler
实例)。
2.2版是最新的(它是关于PyPI的)。您的代码应该可以正常使用它。