How To Fetch Any User’s Tweets Using Python, Tweepy, and the Twitter API

Eli Williams
2 min readNov 15, 2022

--

Photo via Pexels by Tina Nord

Here is a straightforward script I wrote to fetch the maximum number of Tweets allowed by the baseline Twitter API and save it to a .csv file. As of this writing, you can only fetch the most recent 3,200 Tweets for any user, but that is still a useful dataset, and for infrequent Tweeters that may even be their entire history. The script is configured to download all Tweets and replies.

With some minor modifications, this script could be used to check for deleted Tweets, run periodically via cron job to maintain a running list of Tweets, or any similar use cases.

The only non-standard library you’ll need to install is Tweepy, which bills itself as “An easy-to-use Python library for accessing the Twitter API,” and I have found that to be the case.

Step-by-step instructions

  1. Go HERE and sign up for the Twitter API, create an app, and generate a Bearer Token
  2. python -m pip install tweepy at the command line to install Tweepy
  3. Add your Bearer Token and desired username to the below script, run it, and voila!
import csv
import datetime
import tweepy # https://github.com/tweepy/tweepy
import os

# Sign up for the Twitter API and generate a Bearer Token here:
# https://developer.twitter.com/en/docs/twitter-api

TWITTER_BEARER_TOKEN = "PASTE_YOUR_BEARER_TOKEN_HERE"
username = "jack"


def get_tweets(username: str):
"""
Pulls 3,200 (maximum allowed) most recent tweets for specified username and saves to tweets_<username>.csv
"""

client = tweepy.Client(TWITTER_BEARER_TOKEN)
user_id = client.get_user(username=username).data.id
responses = tweepy.Paginator(client.get_users_tweets, user_id, max_results=100, limit=100)
tweets_list = [["link", "username" "tweet"]]
currentime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

counter = 0
for response in responses:
counter += 1
print(f"==> processing {counter * 100} to {(counter + 1) * 100} of {username}'s tweets")
try:
for tweet in response.data: # see any individual tweet by id at: twitter.com/anyuser/status/TWEET_ID_HERE
tweets_list.append([f"https://twitter.com/anyuser/status/{tweet.id}", username, tweet.text])
except Exception as e:
print(e)

with open(f"tweets_{username}_{currentime}.csv", "w", encoding="utf-8", newline='') as f:
writer = csv.writer(f)
writer.writerows(tweets_list)

print("Done!")


if __name__ == "__main__":
os.chdir(os.path.dirname(os.path.abspath(__file__)))
get_tweets(username)

If you enjoyed this content and are interested in coding or crypto, please follow me @elitwilliams on Twitter!

--

--