Who follows who on Twitter!
Part 1

Get the following of a twitter account.
PYTHON
BASIC
HANDS-ON
by Tbx
11 September 2021

In this tutorial we will be getting data from Twitter. Our goal here will be to get the "following" account of a given twitter account. The targeted twitter account will be @solana one of the last trending crypto out there. Please don't ask me why this account...πŸ™„

Get started

As mentionned above we will be using Twitter. Therefore Twitter most recent API will be used: Twitter-v2. What you will need is a twitter developer account and a Bearer Token. That's it we are ready to go. As usual we will be using our favorite programming language python.

πŸš€ Get twitter user id

We start by extraction the user_id associated to a twitter username. We need the use id to actually be able to retrieve data from Twitter. Our main function will be defined as follow:


The function get_user_id is composed of two operations. The first function generates the url. In this function we mentionned the username and the parameters we want to retrieve:


The second operations is where we query the twitter api, by using the package request and our bearer token:
We end ud with the following data

Get the following

Now that we have extracted the username from the user id, we can now retrieve all the account followed by solana twitter account.

The function bellow creates the url that will be used to extract the following associated to a twitter user_id. Since we can only return a maximum of 1000 following accounts, twitter send us back a token page that enables us to retreive the next batch of username.

The next function query the api and return the data in a json format:

Below the top(highest volume of followers) 10 accounts followed by Solana:

Conclusion

In this article we have learnt to get data from twitter api, and performs some insight full plots using python. In the part 2 we will see who one can perform some visualization on the 'following' data using matplotlib library.



🧬 Full code can be found here.


πŸ€“ References