Use Python to Grab Twitter Data: Easiest Guide to Tweepy API

Paul Yang - November 10th, 2022

How to use Tweepy to query the Twitter API with Python

Twitter is a rich repository of public-facing data on every topic imaginable. You can access that dataset to create novel analysis using Tweepy, the API provided to Python users.

You can easily get a Twitter API key from https://developer.twitter.com/ and you can get access to a whopping 500K tweets a month for free!

There is full developer documentation at https://www.tweepy.org/ but you can use the below example as the quickest and easiest way to bring data from Twitter into a data frame and start working in Python with it.

Code example to use Tweepy

!pip install Tweepy
import tweepy as tw

token = 'YOUR_BEARER_TOKEN'
client = tw.Client(bearer_token=token)

# What to search for
query = '#yankees'
max_results = 100 

# We grab tweets + context annotations + create date + user name of tweeter
tweets = client.search_recent_tweets(query=query, tweet_fields=['context_annotations', 'created_at'],user_fields=['name'],  expansions='author_id',  max_results=max_results)

#Return the tweet data as a dataframe
df2 = pd.DataFrame(tweets.data).astype(str)

# Collect the user IDs
df_users = pd.DataFrame(tweets.includes['users'])


Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators