This python script is used to fetch the required number of tweets of a particular Hashtag through your Twitter account and generates an
- 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.
- 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
- Visit Twitter Developer Account Website and click on Create app as shown below
- Click on Apply
- Based on your choice select the Reason for using Twitter developer tools and Click on Next
- Enter your country and what you would call and Click on Next
- 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
- 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
- Fill the required fields and click on Create
- 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:
- Save the script in the local machine.
- Update the API and Access token Keys in the Python script which generated in step8
- 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
Code language: CSS (css)
pip install -r requirements.txt
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.
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(api.search,q=hastag,count=100, lang="en", since="2017-04-03").items(): print (tweet.created_at, tweet.text) csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])