import tweepy import json import time #Twitter API credentials consumer_key = "tUnfouXORfnaVNEAyTrLmW2ZU" consumer_secret = "Yxmd1sLKqp2YwXzJ5IJjaVO6PtrOeq1lKyl5AS2Zu2zktjYZKQ" access_key = "1215780002-2fC55jHbZ4X7NDHgKFJMO1g63Aw0jn1zdmhJjs8" access_secret = "MJfwXrZ9hKvfb8EUba7eoKlu5BIPDwRDKAXHZOBPdPc2p" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) #refer http://docs.tweepy.org/en/v3.2.0/api.html#API #tells tweepy.API to automatically wait for rate limits to replenish #Put your search term searchquery = "#Demonetisation" users =tweepy.Cursor(api.search,q=searchquery).items() count = 0 errorCount=0 file = open('search.json', 'w') while True: try: user = next(users) count += 1 except tweepy.TweepError: print ("sleeping....") user = next(users) except StopIteration: break try: print ("Writing to JSON tweet number:"+str(count)) json.dump(user._json,file,sort_keys = True,indent = 4) except UnicodeEncodeError: errorCount += 1 print ("UnicodeEncodeError,errorCount ="+str(errorCount)) print ("completed, errorCount ="+str(errorCount)+" total tweets="+str(count)) #todo: write users to file, search users for interests, locations etc.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)