Python Tweepy – Getting the number of friends of a user
In this article we will see how we can get the number of friends of a user. A friend is an account which the user is following. The friends_count attribute provides us with and integer donating the number of friends the user account has.
Identifying the number of friends in the GUI :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In the above mentioned profile the number of friends are : 11
In order to get the number of friends we have to do the following :
- Identify the user ID or the screen name of the profile.
- Get the User object of the profile using the
get_user()method with the user ID or the screen name.
- From this object, fetch the friends_count attribute present in it.
Example 1: Consider the following profile :
We will use the user ID to fetch the user. The user ID of the above mentioned profile is 57741058.
The number of friends of the user are : 11
Example 2: Consider the following profile :
We will use the screen name to fetch the user. The screen name of the above mentioned profile is PracticeGfG.
The number of friends of the user are : 5