-
-
Save jenna-matthews/7c01ed34699507cb7904f8f57b0938c0 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 hidden or 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