-
-
Save suraj-deshmukh/b318ced868a14dc0fa1a to your computer and use it in GitHub Desktop.
A script to download all of a user's tweets into a csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd #to store tweets into csv | |
import tweepy | |
#Twitter API credentials | |
consumer_key = "" | |
consumer_secret = "" | |
access_key = "" | |
access_secret = "" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_key, access_secret) | |
api = tweepy.API(auth) | |
def get_all_tweets(screen_name): | |
alltweets = [] | |
new_tweets = api.user_timeline(screen_name = screen_name,count=200) | |
alltweets.extend(new_tweets) | |
oldest = alltweets[-1].id - 1 | |
while len(new_tweets) > 0: | |
print "getting tweets before %s" % (oldest) | |
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest) | |
alltweets.extend(new_tweets) | |
oldest = alltweets[-1].id - 1 | |
print "...%s tweets downloaded so far" % (len(alltweets)) | |
data=[[obj.user.screen_name,obj.user.name,obj.user.id_str,obj.user.description.encode("utf8"),obj.created_at.year,obj.created_at.month,obj.created_at.day,"%s.%s"%(obj.created_at.hour,obj.created_at.minute),obj.id_str,obj.text.encode("utf8")] for obj in alltweets ] | |
dataframe=pd.DataFrame(data,columns=['screen_name','name','twitter_id','description','year','month','date','time','tweet_id','tweet']) | |
dataframe.to_csv("%s_tweets.csv"%(screen_name),index=False) | |
if __name__ == '__main__': | |
#pass in the username of the account you want to download | |
get_all_tweets("TimesNow") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some of the indentations needed to be adjusted (lines 27-29), but then it ran fine.
I didn't need to hard-code a directory path -- the csv file was created in the same location as the python file itself.
FYI -- if you are running Python 3 -- the print commands will need a parentheses after print and at the end of the line, so
print "getting tweets before %s" % (oldest)
becomes
print ("getting tweets before %s" % (oldest))
Thanks for the file :)