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'])
About
Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.