Aug 21, 2019

Twitter Search API Using Python - Codersarts

Updated: Jul 18, 2021

Twitter Search API Using Python - Codersarts

Introduction:

In this Blog, we will Create an application which uses the Twitter Search API to fetch tweets from Twitter using Twitter's API.

To connect to Twitter’s API, we will be using a Python library called Tweepy, which we’ll install in a bit.

Installing Tweepy

Tweepy is an excellently supported tool for accessing the Twitter API. It supports Python 2.6, 2.7, 3.3, 3.4, 3.5, and 3.6.

There are a couple of different ways to install Tweepy.

The easiest way is using pip.

USING PIP

Simply type pip install tweepy into your terminal.

USING GITHUB

You can follow the instructions on Tweepy’s GitHub repository. The basic steps are as follows:

git clone https://github.com/tweepy/tweepy.git
 
cd tweepy
 
python setup.py install
 

You can troubleshoot any installation issues there as well.

We will fetch some tweets, store them into a DataFrame and do some interactive visualization to get some insights.

In order to access Twitter's API you need to have the below:

  • Consumer API key

  • Consumer API secret key

  • Access token

  • Access token secret

In order to get these these keys, You'll have to create a developer account on the Twitter apps site.

Log in or make a Twitter account at https://apps.twitter.com/.

Create a new app (button on the top right)

It is a very simple 5-minute process to get those.

After you create an app and fill-in the app details, you can just go into your app details, go to the keys and tokens tab, click generate, and you'll have your keys and tokens.

Getting the Tweets

We start by storing our keys and tokens in variables

access_token = 'paste your token here'

access_token_secret = 'paste your token secret here'

consumer_key = 'paste your consumer key here'

consumer_secret = 'paste your consumer secret here'

Next we import the libraries we'll use for now:

import tweepy

import numpy as np

import pandas as pd

Now we authenticate using our credentials as follows:

# Creating the authentication object

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

# Setting your access token and secret

auth.set_access_token(access_token, access_token_secret)

# Creating the API object while passing in auth information

api = tweepy.API(auth)

Example 1:

Let's see what people are tweeting about "Machine Learning"

for tweet in api.search('Machine Learning'):

print(tweet.text)

Twitter Search API Using Python

Example 2:

Fetching the ten most recent tweets from your Twitter feed

so do this we'll have to use the API object’s home_timeline() function.

Using the API object to get tweets from your timeline, and storing it in a variable called public_tweets


 
public_tweets = api.home_timeline()


 
# foreach through all tweets pulled
 
for tweet in public_tweets:
 
# printing the text stored inside the tweet object
 
print(tweet.text)

Using the API object to get tweets from your timeline

Recommended Posts: