├── README.md └── get_tweets.py /README.md: -------------------------------------------------------------------------------- 1 | ## Python Script to Download Tweets 2 | 3 | Use the script `get_tweets.py` to download the last 100 tweets (or whatever number you choose) from any Twitter user. 4 | 5 | ### Setup 6 | (1) You need to get your Twitter API Credentials by creating a new app at [developer.twitter.com](developer.twitter.com). 7 | 8 | (2) This script uses Tweepy. If you don't have Tweepy installed you need to run this command on the terminal first: 9 | 10 | ``` 11 | $ sudo pip install tweepy 12 | ``` 13 | 14 | ### Clone and use the Python script 15 | Now you're ready to clone and use the get_tweets script. 16 | 17 | ``` 18 | $ git clone https://github.com/gitlaura/get_tweets.git 19 | $ cd get_tweets 20 | ``` 21 | Enter the appropriate keys from your Twitter app in lines 11-15 of `get_tweets.py` using any text editor. 22 | 23 | Finally you can run the script by entering one Twitter username at the command line: 24 | 25 | ``` 26 | $ python get_tweets.py [twitter_username] 27 | ``` 28 | 29 | #### More details on my blog: 30 | [http://www.getlaura.com/how-to-download-tweets-from-the-twitter-api/](http://www.getlaura.com/how-to-download-tweets-from-the-twitter-api/) 31 | -------------------------------------------------------------------------------- /get_tweets.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | import csv 6 | 7 | #http://www.tweepy.org/ 8 | import tweepy 9 | 10 | #Get your Twitter API credentials and enter them here 11 | consumer_key = "your_consumer_key" 12 | consumer_secret = "your_consumer_secret" 13 | access_key = "your_access_key" 14 | access_secret = "your_access_secret" 15 | 16 | #method to get a user's last tweets 17 | def get_tweets(username): 18 | 19 | #http://tweepy.readthedocs.org/en/v3.1.0/getting_started.html#api 20 | auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 21 | auth.set_access_token(access_key, access_secret) 22 | api = tweepy.API(auth) 23 | 24 | #set count to however many tweets you want 25 | number_of_tweets = 100 26 | 27 | #get tweets 28 | tweets_for_csv = [] 29 | for tweet in tweepy.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets): 30 | #create array of tweet information: username, tweet id, date/time, text 31 | tweets_for_csv.append([username, tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")]) 32 | 33 | #write to a new csv file from the array of tweets 34 | outfile = username + "_tweets.csv" 35 | print "writing to " + outfile 36 | with open(outfile, 'w+') as file: 37 | writer = csv.writer(file, delimiter=',') 38 | writer.writerows(tweets_for_csv) 39 | 40 | 41 | #if we're running this as a script 42 | if __name__ == '__main__': 43 | 44 | #get tweets for username passed at command line 45 | if len(sys.argv) == 2: 46 | get_tweets(sys.argv[1]) 47 | else: 48 | print "Error: enter one username" 49 | 50 | #alternative method: loop through multiple users 51 | # users = ['user1','user2'] 52 | 53 | # for user in users: 54 | # get_tweets(user) 55 | --------------------------------------------------------------------------------