streaming.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import absolute_import, print_function
  2. from tweepy.streaming import StreamListener
  3. from tweepy import OAuthHandler
  4. from tweepy import Stream
  5. # Go to http://apps.twitter.com and create an app.
  6. # The consumer key and secret will be generated for you after
  7. consumer_key=""
  8. consumer_secret=""
  9. # After the step above, you will be redirected to your app's page.
  10. # Create an access token under the the "Your access token" section
  11. access_token=""
  12. access_token_secret=""
  13. class StdOutListener(StreamListener):
  14. """ A listener handles tweets that are received from the stream.
  15. This is a basic listener that just prints received tweets to stdout.
  16. """
  17. def on_data(self, data):
  18. print(data)
  19. return True
  20. def on_error(self, status):
  21. print(status)
  22. if __name__ == '__main__':
  23. l = StdOutListener()
  24. auth = OAuthHandler(consumer_key, consumer_secret)
  25. auth.set_access_token(access_token, access_token_secret)
  26. stream = Stream(auth, l)
  27. stream.filter(track=['basketball'])