Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to download tweets by hashtags
- import time
- import json
- # http://www.tweepy.org/ or pip install tweepy
- from tweepy import Stream
- from tweepy import OAuthHandler
- from tweepy.streaming import StreamListener
- # Get your Twitter API credentials and enter them here
- consumer_key = ''
- consumer_secret = ''
- access_key = ''
- access_secret = ''
- # Counter indicated numver of tweets downloaded
- Counter = 0
- class listener(StreamListener):
- def on_data(self,data):
- global Counter
- # parsing the tweets and writing it to csv file \
- # for the program we have stored only 5 fields but you can add more
- with open('DataDL2.csv','a') as tf:
- temp = json.loads(data)
- user = temp["user"]
- output_string = "{0}, {1}, {2}, {3}, {4}".format(temp["text"], user["screen_name"], user["favourites_count"], temp["created_at"], temp["retweeted"])
- tf.write(output_string + "\n")
- Counter += 1
- if Counter % 5 == 0:
- print(Counter)
- return True
- def on_error(self,status):
- print(status)
- print("ON ERROR")
- while True:
- try:
- auth =OAuthHandler(ckey,csecret)
- auth.set_access_token(atoken,asecret)
- print("Data collection started : ")
- twitterStream = Stream(auth,listener())
- # keywords to track in tweets
- # add your own keywords in the below list
- twitterStream.filter(track = ['#apple', '#google', '#samsung', '#sony' ])
- except Exception:
- pass
Add Comment
Please, Sign In to add comment