Using the Twitter API with Python and Tweepy to get public data on any account

0

[ad_1]

Twitter is a popular social media website with millions of users. It also has a powerful application user interface or API that developers can access. In this tutorial, let’s explore how to access the Twitter API using the Python programming language and Tweepy to get followers, following and tweet count for any account.

Python, Tweepy and the Twitter API

Tweepy is an open-sourced, easy-to-use Python library for accessing the Twitter API. It has excellent documentation and is very beginner friendly. The Twitter API allows you to do various things depending on your permissions.

This tutorial assumes you already know the basics of Python, already applied for a free developer account to get access to the Twitter API and installed the Tweepy library with pip. In a new python file, let’s start by importing Tweepy.

For accessing any public data on Twitter, we will only need a Bearer Token which you will get after signing up for a developer account on Twitter and registering an app.

client = tweepy.Client("insert bearer token here")

After that, we will declare a variable to store the username of any Twitter user profile we want to access the public data of.

Next let’s feed the user profile name via Tweepy to Twitter, tell it that we want the public data fields of that profile and save that data array to a variable.

data = client.get_user(username=user,user_fields=["public_metrics"])

With the data saved, we can then pick out the three specific data types we want and save them into their own variables and print them to the screen to check.

followers = data[0].public_metrics['followers_count']
following = data[0].public_metrics['following_count']
tweets = data[0].public_metrics['tweet_count']

print(user)
print(followers)
print(following)
print(tweets)

Good job! You have now successfully used the Twitter API with Python and Tweepy to get public data on Twitter. Remember you can modify the user variable to any public account on Twitter and get their info. Let’s put the code altogether and format a nice message. You can do so much more with the Twitter API such as sending tweets and more with your account. Please comment below if that is a tutorial you would be interested in.

The code from this tutorial is available on Github. Follow @Geezam on Twitter and subscribe to our Youtube channel for more tech tutorials, howtos and walkthroughs.

import tweepy

client = tweepy.Client("insert bearer token here")

user = "Geezam"

data = client.get_user(username=user,user_fields=["public_metrics"])

followers = data[0].public_metrics['followers_count']
following = data[0].public_metrics['following_count']
tweets = data[0].public_metrics['tweet_count']

print(user)
print(followers)
print(following)
print(tweets)

message = "Twitter user @" + str(user) + " has " 
+ str(followers) + " Followers, follows " 
+ str(following) + " accounts and has sent " 
+ str(tweets) + " Tweets."

print (message)



[ad_2]

Source link

Leave A Reply

Your email address will not be published.