Python to Fetch and Store Tweets with Full Source Code

This python script is used to fetch the required number of tweets of a particular Hashtag through your Twitter account and generates an .csv file.


  • tweepy==3.9.0


  1. You need any Python IDE to run this script. You can visit here to download the latest version of Python based on your operating System.
  2. Next to execute this script you have to get the API keys from twitter developer account by creating an app at Twitter developer account. For creating an app at Twitter developer account you have to follow the below steps

Step 1:

Step 2:

  • Click on Apply

Step 3:

  • Based on your choice select the Reason for using Twitter developer tools and Click on Next

Step 4:

  • Enter your country and what you would call and Click on Next

Step 5:

  • Now Carefully read the questions and answer all the questions as per the requirement.
  • Because based on your answer only the account approval will be taken place

Step 6:

  • If your account is successfully approved you can come to the homepage Twitter developer account and now click on Create an app icon on the right top.

Note: sometimes it takes time to approve the developer account based on step-5 answers

Step 7:

  • Fill the required fields and click on Create

Step 8:

  • After creating the App click on the Keys and tokens option you will observe Consumer and Access token keys.
  • Copy them and paste them into the python script

How to Run:

  1. Save the script in the local machine.
  2. Update the API and Access token Keys in the Python script which generated in step8
  3. Open a terminal/command prompt and change directory location to the folder where your script is located and run the below commands

To install required packages run the below command

pip install -r requirements.txt
Code language: CSS (css)

To execute the script run the following command

Code language: CSS (css)

After successful execution of the script you should observe the below output in command prompt/terminal as well as one .csv file with the required number of tweets will be generated.

Final generated tweets in.csv file output

Source Code:

import tweepy
import csv

# input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
hastag = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

# Open/Create a file to append data
csvFile = open('tweets.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

for tweet in tweepy.Cursor(,q=hastag,count=100,
    print (tweet.created_at, tweet.text)
    csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

Leave a Comment