Twitter Search API Code Template 🐦
Hi. This notebook contains the code templates used in my Medium article on the data science communty's behavior during COVID-19. Feel free to use any of the templates below to conduct your own Twitter analytics project.
Step 1: Sign up for a developer account
In order to access Twitter's APIs, one first needs to sign up for a developer account here. Describe your use case and wait for Twitter to confirm your account (which usually only takes a few hours at max).
Step 2: Credentials
Once you've signed up, you can access Twitter's developer console. Depending on your use case, choose whatever API (or combination thereof) helps you the most. To access the respective endpoint, one can either use a YAML file or environment variables.
Since Deepnote enables you to easily create environment variables in the integrations menu tab, it'd make sense to stick with that in our case. Using bash or zsh, setting environment variables is also very easy to do:
For Twitter's APIs we need to set the following:
Find the respective values either in your Twiter dev console or the docs for the API you'd like to use (e.g. what endpoint to use)
Step 3: So many tweets
Once you've finished dealing with credentials, you can finally start using the APIs. I used Twitter Search APIs and, therefore, stuck to using the Python wrapper published by Twitter itself. However, there are many other wrappers out there that, depending on your use case, can also do the job.
Install the wrapper with
pip install searchtweets
Using this wrapper, the work flow is more or less the same each time:
- Define a rule (e.g. what hashtags or authors to search for)
- Retrieve data based on rule
All this does is generate a dictionary for you that will be used to query the API for tweets containing the hastag #covid19. Be mindful that what you can do here depends on your quotas.
This is more or less all you need to know to get started. In this case,
covid19_tweets will be a Tweet object providing you with tons of information both about the tweet and the author.