使用 tweepy 遍历 Twitter 流
iterate through a Twitter stream using tweepy
我有这段代码可以在 tweepy 中创建一个 Stream(我的目标是获得一个人的所有提及的流):
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
def on_error(self, status_code):
if status_code == 420:
#returning False in on_data disconnects the stream
return False
l = StreamListener()
stream = tweepy.Stream(auth = api.auth, listener=l)
我想知道如何将每个提及项与 tweepy.Stream
对象一起使用?
我试过类似的东西:
for mention in tweepy.Cursor(stream.filter(track=['@twitter'])).items():
*code*
但这只会创建一个我无法操纵或使用的流。我对可以使用任何提及该人的方式感兴趣吗?
提前致谢。
我发现只需将从流中获取的数据转换为 json 即可让我使用这些数据!
class StreamListener(tweepy.StreamListener):
def on_data(self, data):
data = json.loads(data)
print(data.text)
def on_error(self, status_code):
if status_code == 420:
#returning False in on_data disconnects the stream
return False
我有这段代码可以在 tweepy 中创建一个 Stream(我的目标是获得一个人的所有提及的流):
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
def on_error(self, status_code):
if status_code == 420:
#returning False in on_data disconnects the stream
return False
l = StreamListener()
stream = tweepy.Stream(auth = api.auth, listener=l)
我想知道如何将每个提及项与 tweepy.Stream
对象一起使用?
我试过类似的东西:
for mention in tweepy.Cursor(stream.filter(track=['@twitter'])).items():
*code*
但这只会创建一个我无法操纵或使用的流。我对可以使用任何提及该人的方式感兴趣吗? 提前致谢。
我发现只需将从流中获取的数据转换为 json 即可让我使用这些数据!
class StreamListener(tweepy.StreamListener):
def on_data(self, data):
data = json.loads(data)
print(data.text)
def on_error(self, status_code):
if status_code == 420:
#returning False in on_data disconnects the stream
return False