December 29, 2025

1 thought on “How to download 3200 of a user’s tweets

  1. To collect from a list of usernames defined in a file :

    import sys
    import csv
    import json
    import os
    import tweepy as tw
    import pandas as pd
    from datetime import datetime

    bearer_token = “” \
    ” ”

    consumer_key = ” # API key
    consumer_secret = ” # API Secret Key
    access_token = ”
    access_token_secret = ”

    auth = tw.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tw.API(auth, wait_on_rate_limit=True)

    def save_json(file_name, file_content):
    with open(path + file_name, ‘w’, encoding=’utf-8′) as f:
    json.dump(file_content, f, ensure_ascii=False, indent=4)

    if (len(sys.argv) < 2):
    print ("Usage : tweetcollect “)
    sys.exit()

    #Amend path to a path suitable for your system.
    path = ‘/home/shizoor/PycharmProjects/tweetcollector/fash/’+datetime.now().strftime(‘%d-%m-%Y%H%M%S’)
    listfile = sys.argv[1]

    text_file = open(listfile, “r”)
    usernames=text_file.read().split(“\n”)
    print (“Usernames : “, usernames)
    text_file.close()

    def get_all_tweets(screen_name):
    # initialize a list to hold all the Tweets
    alltweets = []
    outtweets=[]
    # make initial request for most recent tweets
    # (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name=screen_name, count=200, tweet_mode=’extended’)
    # save most recent tweets
    alltweets.extend(new_tweets)
    # save the id of the oldest tweet less one to avoid duplication
    try:
    oldest = alltweets[-1].id – 1
    except:
    print(“exception! Contents of alltweets:”, alltweets)
    # keep grabbing tweets until there are no tweets left
    while len(new_tweets) > 0:
    print(“getting tweets before %s” % (oldest))
    # all subsequent requests use the max_id param to prevent
    # duplicates
    new_tweets = api.user_timeline(screen_name=screen_name, count=200, max_id=oldest, tweet_mode=’extended’)
    # save most recent tweets
    alltweets.extend(new_tweets)
    # update the id of the oldest tweet less one
    oldest = alltweets[-1].id – 1
    print(“…%s tweets downloaded so far” % (len(alltweets)))
    ### END OF WHILE LOOP ###
    # transform the tweepy tweets into a 2D array that will
    # populate the csv
    #outtweets = [[tweet.id_str, tweet.created_at, tweet.extended_tweet[“full_text”], tweet.favorite_count, tweet.in_reply_to_screen_name,
    # tweet.retweeted] for tweet in alltweets]

    for tweet in alltweets:
    outtweet = [tweet.id_str, tweet.created_at, ”, tweet.favorite_count, tweet.in_reply_to_screen_name,tweet.retweeted, tweet.retweet_count, ‘https://www.twitter.com/’+screen_name+’/status/’+tweet.id_str]
    try:
    outtweet[2] = tweet.retweeted_status.full_text
    except:
    outtweet[2] = tweet.full_text
    outtweets.append(outtweet)

    # write the csv
    with open(path + ‘%s_tweets.csv’ % screen_name, ‘w’) as f:
    writer = csv.writer(f)
    writer.writerow([“id”, “created_at”, “text”, “likes”, “in reply to”, “retweeted”, “retweet count”, “uri”])
    writer.writerows(outtweets)
    print(“csv written to: “+path + ‘%s_tweets.csv’ % screen_name)
    pass

    def tweetsend(tweettext):
    api.update_status(tweettext)

    if __name__ == ‘__main__’:

    for username in usernames :
    print (‘Getting all tweets from ‘+username)
    if len(username)>0:
    try:
    get_all_tweets(username)
    except:
    print(“tweepy error”)
    else:
    print(“empty username!”)

Leave a Reply to Richard Ferris Cancel reply

Your email address will not be published. Required fields are marked *