Daily Python
Published in

Daily Python

Python script to fetch tweets based on hashtags(#)| Daily Python #2

This article is a tutorial on how to fetch tweets based on given hashtags(#) using Python.

Requirements:

  1. Python 3.0
  2. Pip

Install the following packages:

  1. tweepy — Library that provides easy access to Twitter APIs
pip install tweepy

Let’s start by importing this library in our code.

import tweepy

In order to access twitter APIs through tweepy, we first need to create a twitter application. For this search ‘ Twitter Application Management’ and go to the developer website.

Snip of google search for Twitter App Management

You will be required to log in for this and you will need to accept some T&Cs for a developer account. Once your developer account is set up, you will be redirected to your dashboard. Here you will have an option for creating your own application.

Snip of developer account (Twitter)

As shown above, you will be prompted to fill the required fields, such as the application name (which should be unique), description, etc. You will also be asked to provide your website URL. I use my Github profile link for that purpose. Furthermore, you will be prompted to describe how the application will be used.

Twitter Developer terms for creating an application

After you have successfully created the application, you will be redirected to the following screen.

Snip of application dashboard (Twitter)

Now, navigate to the ‘Key and tokens’ tab, where you will be able to access your unique consumer API key and API secret key. Also, you need to generate the Access token key and Access token secret key.

These four keys are basically used for authentication purposes. If you wish to read more about it, you can search for ‘OAuth’ or you can visit here.

Snip of Keys and tokens (Twitter)

DO NOT share these API keys and Access token keys, also make sure you remove them while pushing your code on any version control e.g. Github.

Let’s write the code to handle fetch the tweets based on any given hashtag(#).

Store your API keys and Access tokens in the variables as given below:

consumerKey = “YOUR_CONSUMER_KEY”
consumerSecret = “YOUR_CONSUMER_SECRET_KEY”
accessToken = “YOUR_ACCESS_TOKEN”
accessTokenSecret = “YOUR_ACCESS_TOKEN_SECRET”

Now, we will create a class to handle the twitter stream. This class will inherit the StreamListener class provided by ‘tweepy.stream’

class TwitterStreamListener(tweepy.streaming.StreamListener):    ''' Handles data received from the stream. '''    def on_status(self, status):
print(status.id)
print(status.user.name)
print(status.text)
return True

def on_error(self, status_code):
print('Got an error with status code: ' + str(status_code))
return True # To continue listening

def on_timeout(self):
print('Timeout...')
return True # To continue listening

Let me explain the above code. We created a class named TwitterStreamListener which inherits the StreamListener class. We override the ‘on_status’, ‘on_error’, and ‘on_timeout’ functions that were initially defined in the StreamListener class provided by ‘tweepy’.

Let’s discuss the ‘on_error’ function, which is self-explanatory. This function is triggered when there is an error while listening to the Twitter stream. You can write your own error message in this function. The incoming parameter status_code contains what type of error has occurred. E.g. 404,503,etc.

The ‘on_timeout’ function is triggered when there is any network issue and the listener cannot connect to the twitter stream.

Now, the main function, ‘on_status’ — this is the function that gets the tweets from the stream into the ‘status’ object. The structure of the response stored in status can be found on the Twitter docs.

We can access the elements using the keys given directly through the status object. As shown in the above code, we print the Tweet ID, the text and the user name. As the username is a part of the sub-document, we access it in that order.

print(status.user.name)

‘status’ has ‘user’ and ‘user’ has ‘name’.

Now we use the above class TwitterStreamListener to fetch the tweets.

listener = TwitterStreamListener()auth = tweepy.OAuthHandler(consumerKey, consumerSecret)auth.set_access_token(accessToken, accessTokenSecret)stream = tweepy.streaming.Stream(auth, listener)stream.filter(track=["#Python"])

First, we create an object of the TwitterStreamListener class. We also create an object of the OAuthHandler provided by ‘tweepy’. Here, we use the Consumer Key and the Consumer Secret Key that we got after creating an application on Twitter. We also, provide this OAuthHandler the access tokens that we generated for our application.

Finally, we create an object of the Stream class and provide it our ‘auth’ and ‘listener’ objects. We provide the filter of our choice, here I search for tweets with #Python.

Snip of the output

We have successfully fetched tweets for the given hashtag. I hope this article was helpful, do leave some claps if you liked it.

Open to suggestions, do not hesitate to give your valuable feedback.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store