【发布时间】:2020-05-08 03:19:51
【问题描述】:
我一直在尝试使用 tweepy 流式传输推文并打印它们。我想按用户和关键字过滤推文,但它似乎不起作用。当我使用以下代码时,当我只希望来自特定用户/用户时,我会使用关键字“hi”从所有用户那里获取所有推文。但是,当我不按关键字过滤时,我会从特定用户/用户那里获得所有传入的推文。
from __future__ import absolute_import, print_function
from tweepy import OAuthHandler, Stream, StreamListener
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key = "..."
consumer_secret = "..."
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token = "..."
access_token_secret = "..."
class StdOutListener(StreamListener):
""" A listener handles tweets that are 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__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=['hi'], follow=["userID"])
【问题讨论】:
标签: python python-3.x twitter tweepy