|
| 1 | +""" |
| 2 | +Created on Sun Oct 04 23:10:41 2015 |
| 3 | +@author: ujjwal.karn |
| 4 | +""" |
| 5 | + |
| 6 | +#first, install pip by following instructions here: http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows |
| 7 | +#then, to install tweepy library, go to Anaconda command prompt and type: pip install tweepy |
| 8 | +#once tweepy is installed, run the codes below: |
| 9 | + |
| 10 | +import tweepy #this will give an error if tweepy is not installed properly |
| 11 | +from tweepy import OAuthHandler |
| 12 | + |
| 13 | +#provide your access details below |
| 14 | +access_token = "xxxxxxxx" |
| 15 | +access_token_secret = "xxxxxxxx" |
| 16 | +consumer_key = "xxxxxxxx" |
| 17 | +consumer_secret = "xxxxxxxx" |
| 18 | + |
| 19 | +auth = OAuthHandler(consumer_key, consumer_secret) |
| 20 | +auth.set_access_token(access_token, access_token_secret) |
| 21 | + |
| 22 | +api = tweepy.API(auth) |
| 23 | + |
| 24 | +from tweepy import Stream |
| 25 | +from tweepy.streaming import StreamListener |
| 26 | + |
| 27 | +class MyListener(StreamListener): |
| 28 | + |
| 29 | + def on_data(self, data): |
| 30 | + try: |
| 31 | + with open('C:\\Users\\ujjwal.karn\\Desktop\\Tweets\\python.json', 'a') as f: #change location here |
| 32 | + f.write(data) |
| 33 | + return True |
| 34 | + except BaseException as e: |
| 35 | + print("Error on_data: %s" % str(e)) |
| 36 | + return True |
| 37 | + |
| 38 | + def on_error(self, status): |
| 39 | + print(status) |
| 40 | + return True |
| 41 | + |
| 42 | +twitter_stream = Stream(auth, MyListener()) |
| 43 | + |
| 44 | +#change the keyword here |
| 45 | +twitter_stream.filter(track=['#cricket']) |
0 commit comments