How To Create a Twitter App and API Interface Via Python

How To Create a Twitter App and API Interface Via Python

This tutorial illustrates how to use a Python API to connect to a Twitter account using the Twitter library. Specifically, this API allows a user to extract high quantities of data pertaining to a specific Twitter account, as well as directly control Twitter posts from the Python platform (such as posting multiple tweets at once).

Even if you’re not a regular Python user, using one of Python’s Twitter libraries is quite useful when it comes to analytics. For instance, while web developers might be more inclined to use a language such as PHP in connecting to the API, Python offers more flexibility in analyzing trends and statistics with the data. Therefore, data scientists and other analysts would find Python to be a better fit for this purpose.

In this tutorial, we’ll start with some basic steps in connecting Python to the Twitter API, and then look at how to stream the desired data. Note that, while the Twitter library (and other Python libraries such as Tweepy and Twython) can carry out a multitude of different tasks with the data, I’ll focus on some of the more basic (and useful) queries in this article. Specifically, I’ll walk through how to:

  1. connect Python to the Twitter API using the appropriate credentials
  2. download tweets associated with the specific account
  3. download a list of all following and followed users for an account
  4. post multiple tweets at once
  5. customize a search for instances of a specific term on Twitter.

1. Connect Python to the Twitter API

This tutorial uses iPython as the Python interface to connect to Twitter. In order to connect to the API, we need to obtain the Consumer Key, Consumer Secret, and Access Token Secret.

To obtain these, you need to sign into your account at apps.twitter.com. Once there, you’ll be prompted to create an application:

create application

Once you have the application created, you’ll find the relevant keys and tokens under the Keys and Access Tokens section.

consumer key

access token

Firstly, we install the python-twitter library in our terminal as follows:

pip install python twitter

Once we’ve done that, we import the Twitter library and enter the credentials as follows:

import twitter
api = twitter.Api(consumer_key='your_consumer_key',
  consumer_secret='your_consumer_secret',
    access_token_key='your_access_token_key',
    access_token_secret='your_access_token_secret')
print(api.VerifyCredentials())

As soon as the correct credentials are entered, the connection to the API is complete, and we’re now able to control our Twitter account through the Python platform!

2. Download User Timeline

Now that we have connected Python to the Twitter API, we can go ahead and start utilizing different Twitter functions remotely. For instance, if we wish to download a user timeline of tweets, we do this using api.GetUserTimeline as below (and specifying the screen name for the appropriate account), and then using the print() function to display the results:

statuses = api.GetUserTimeline(screen_name='Michael Grogan')
print([s.text for s in statuses])

Once we’ve inputted the above, we see the respective timeline displayed in the Python interface:

get user timeline

Continue reading %How To Create a Twitter App and API Interface Via Python%