-
Notifications
You must be signed in to change notification settings - Fork 95
/
extract_twitter_data.py
50 lines (38 loc) · 1.48 KB
/
extract_twitter_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Created on Sun Oct 04 23:10:41 2015
@author: ujjwal.karn
"""
#first, install pip by following instructions here: http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows
#then to install tweepy, go to command prompt and type: pip install tweepy
#once tweepy is installed, run the codes below:
import tweepy #this will give an error if tweepy is not installed properly
from tweepy import OAuthHandler
#provide your access details below
access_token = "xxxxxxxx"
access_token_secret = "xxxxxxxx"
consumer_key = "xxxxxxxx"
consumer_secret = "xxxxxxxx"
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
from tweepy import Stream
from tweepy.streaming import StreamListener
class MyListener(StreamListener):
def on_data(self, data):
try:
with open('C:\\Users\\ujjwal.karn\\Desktop\\Tweets\\python.json', 'a') as f: #change location here
f.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, MyListener())
#change the keyword here
twitter_stream.filter(track=['#cricket'])
#references:
#http://marcobonzanini.com/2015/03/02/mining-twitter-data-with-python-part-1/
#http://adilmoujahid.com/posts/2014/07/twitter-analytics/
#https://github.com/tweepy/tweepy