I trust all is well with everyone here. My apologies if this has been answered before, though I am trying to do the following.
cursor = tweepy.Cursor( api.search_tweets, q = '"Hello"', lang = 'en', result_type = 'recent', count = 2)
I want to match the number in count, to the number of json objects I will be iterating through.
for tweet in cursor.items(): tweet_payload = json.dumps(tweet._json,indent=4, sort_keys=True)
I have tried several different ways to write the data, though it would appear that the following does not work (currently is a single fire):
with open("Tweet_Payload.json", "w") as outfile: outfile.write(tweet_payload) time.sleep(.25) outfile.close()
This is what it looks like put together.
import timeimport tweepyfrom tweepy import cursorimport Auth_Codesimport jsontwitter_auth_keys = {"consumer_key" : Auth_Codes.consumer_key,"consumer_secret" : Auth_Codes.consumer_secret,"access_token" : Auth_Codes.access_token,"access_token_secret" : Auth_Codes.access_token_secret}auth = tweepy.OAuthHandler( twitter_auth_keys["consumer_key"], twitter_auth_keys["consumer_secret"])auth.set_access_token( twitter_auth_keys["access_token"], twitter_auth_keys["access_token_secret"])api = tweepy.API(auth)cursor = tweepy.Cursor( api.search_tweets, q = '"Hello"', lang = 'en', result_type = 'recent', count = 2)for tweet in cursor.items(): tweet_payload = json.dumps(tweet._json,indent=4, sort_keys=True) with open("Tweet_Payload.json", "w") as outfile: outfile.write(tweet_payload) time.sleep(.25) outfile.close()
Edit:Using the suggestion by Mickael
also, the current code
tweet_payload = []for tweet in cursor.items(): tweet_payload.append(tweet._json) print(json.dumps(tweet_payload, indent=4,sort_keys=True)) with open("Tweet_Payload.json", "w") as outfile: outfile.write(json.dumps(tweet_payload,indent=4, sort_keys=True)) time.sleep(.25)
Just loops, I am not sure why thats the case when the count is 10. I thought it would run just 1 call for 10 results or less, then end.