Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to download all tweets of a users on twitter and store in csv file
- import sys
- import csv
- import tweepy # http://www.tweepy.org/ or pip install tweepy
- import json
- # Get your Twitter API credentials and enter them here
- consumer_key = ''
- consumer_secret = ''
- access_key = ''
- access_secret = ''
- # method to get a user's last 200 tweets
- def get_tweets(username):
- # http://tweepy.readthedocs.org/en/v3.1.0/getting_started.html#api
- auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
- auth.set_access_token(access_key, access_secret)
- api = tweepy.API(auth)
- alltweets = []
- #set count to however many tweets you want; twitter only allows 200 at once
- number_of_tweets = 200
- #get tweets
- tweets = api.user_timeline(screen_name = username,count = number_of_tweets)
- #save most recent tweets
- alltweets.extend(tweets)
- #save the id of the oldest tweet less one
- oldest = tweets[-1].id - 1
- #keep grabbing tweets until there are no tweets left to grab
- while len(tweets) > 0:
- print ("getting tweets before %s" % (oldest))
- #all subsiquent requests use the max_id param to prevent duplicates
- tweets = api.user_timeline(screen_name = username, count = 200, max_id=oldest)
- #save most recent tweets
- alltweets.extend(tweets)
- #update the id of the oldest tweet less one
- oldest = alltweets[-1].id - 1
- print ("...%s tweets downloaded so far" % (len(alltweets)))
- # for the example we are extracting only 6 fields
- # You can add to the list and extract more fields
- #transform the tweepy tweets into a 2D array that will populate the csv
- outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8"), tweet.in_reply_to_screen_name, tweet.user.name, tweet.user.screen_name] for tweet in alltweets]
- #write to a new csv file from the array of tweets
- print ("writing to {0}_tweets.csv".format(username))
- with open("{0}_tweets.csv".format(username) , 'w+') as file:
- writer = csv.writer(file, delimiter=',')
- writer.writerows(outtweets)
- if __name__ == '__main__':
- # put the usernames in the below list
- # names = ['user1','user2']
- names = []
- userCount = 0
- for username in names:
- try:
- print("User number = " + str(userCount))
- print("starting with user " + username)
- #get tweets for username passed at command line
- get_tweets(username)
- print ("\nDownloaded Files for " + username + "\n")
- userCount += 1
- except:
- pass
Add Comment
Please, Sign In to add comment