Skip to content

Instantly share code, notes, and snippets.

@vallantin
Created October 16, 2018 15:56
Show Gist options
  • Save vallantin/4118811411007867dc1b28dff0ce0f63 to your computer and use it in GitHub Desktop.
Save vallantin/4118811411007867dc1b28dff0ce0f63 to your computer and use it in GitHub Desktop.
def get_save_tweets(filepath, api, query, max_tweets=1000000, lang='pt'):
tweetCount = 0
#Open file and save tweets
with open(filepath, 'w') as f:
# Send the query
for tweet in tweepy.Cursor(api.search,q=query,lang=lang).items(max_tweets):
#Convert to JSON format
f.write(jsonpickle.encode(tweet._json, unpicklable=False) + '\n')
tweetCount += 1
#Display how many tweets we have collected
print("Downloaded {0} tweets".format(tweetCount))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment