Codementor Events

Data Mining with tweepy

Published May 16, 2019
Data Mining with tweepy

Tweepy is a Python library for accessing Twitter API. It is cool for simple automation. In this tutorial , I will be covering how to get tweets from our timeline. What we will be needing for this tutorial include:

  1. Python3

  2. pip

  3. Jupyter Notebook (for interactive section)

  4. Tweepy

  5. Twitter account.

The first step is to download python 3.7 visit hereand download the interpreter that correspond with the os on your laptop.

Install python3 interpreter on your computer, remember to tick add to system path during your installation.

Getting interesting ?

Now open your terminal or command prompt

Type pip install tweepy or sudo apt install python3-pip for linux users then pip install tweepy

After this install our jupyter notebook

Let’s go to our workspace now

Open your terminal or CMD and type jupyter notebook to start our interactive section

Your browser should open automatically

Let’s start work

Now we will need to import Tweepy

run with shift + enter, immediately a new cell will pop out

Getting Twitter Credentials

Now we need to create a twitter account, go to apps.twitter.com and sign in with your account. Create a Twitter application and generate a Consumer Key, Consumer Secret, Access Token, and Access Token Secret.

The next thing to do is to create variables for your credentials and enter into your new cell as shown below :

Note: your consumer key,consumer secrets, access token,access token secret should be kept private.

Press shift + enter again to run your interactive section, after pressing this a new cell will pop

Getting Tweets

You can get recent tweets from account you follow recently by entering the following into your new cell as shown below:

This will download your timeline tweets and print each in the console.

Now let’s get the number of followers and People you followed recently

The result is:
h_bushroh
244
david_faniyi
Moustapha_6C
mbao_01
OauNacoss
pyconcharlas
DjangoGirlsPyUK
pyconindia
elonmusk
FrontendMasters
Elishatofunmi
intelaiiot
DurexNG
nextdeegit
PrincesOluebube
gitlab
Djangotarkwa
pykidsghana
pydataghana
instadeepai
forloopoau

my username is h_bushroh, I have 244 followers and list of people shown include list of people I followed recently.

How to save data gotten from twitter as csv

This can be done using pandas, a python library.

This will save our fetched data to in csv format.

Things you can do with fetched data

Twitter Sentiment analysis

Auto follow and auto tweet Twitter bot.

Conclusion

I hope that this tutorial will help to get started with data mining using tweepy.

I love feedback please let me know what you think — write a response or just hit the clap button and share this post with friends and colleagues.
Thanks for reading!

Resources include tweepy documentation andjupyter documentation.

Discover and read more posts from Hammed Busirah
get started
post commentsBe the first to share your opinion
Show more replies