Categories
Programming Python

How to get data from twitter using Tweepy in Python?

To start working on Python you need to have Python installed on your PC. If you haven’t installed python. Go to the Python website and get it installed.

After installing Python set up your twitter account if you don’t have one already

Using the twitter account go to Developer Page and Apply for Developer Access and fill the form and read the developer agreement at the end.

You can create the developer account according to your area of interest. I have created using Student.

After Submission, you’ll receive an email for a confirmation.

Confirmation Email

After confirming the email. Your account will be in a review phase and you’ll receive an email of approval after the review.

Once Your Application gets accepted. Create an App to access the tweets.

After, creating the App Successfully go to Keys and Tokens in your app to access the keys.

Access Twitter API in Python

Install Tweepy by using the following command if you haven’t installed it already.

pip install tweepy

Once you have installed Tweepy. Import the relevant libraries to your Python file.

import csv
import tweepy as tw
from tweepy import OAuthHandler

To access the data of tweets you need to have 4 keys from the Twitter app page. You can get the keys from the Key Access Token Tab and define them in your python file as below.

consumer_key= 'your_consumer_key'
consumer_secret= 'your_consumer_secret'
access_token= 'your_access_token'
access_token_secret= 'your_access_token_secret'
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

Next, you need to implement Tweepy cursor to fetch the tweets. I have added them in a nested loop to fetch 100 tweets at a time with 5 iterations with the following Twitter Data.

  • ID
  • Text
  • Created at
with open('tweets.csv', 'wb') as csvFile:
tweetWriter = csv.writer(csvFile, delimiter=',')
for i in range(0,5):
tweets = api.search(q='*', lang='en', count=100)
print ("Loop %d" %i)
for tweet in tweets:
tweetData = [tweet.id,tweet.text.encode('utf-8'), tweet.created_at]
tweetWriter.writerow(tweetData)

Finally, the code will look like this,

import csv
import tweepy as tw
from tweepy import OAuthHandler
consumer_key= 'your_consumer_key'
consumer_secret= 'your_consumer_secret'
access_token= 'your_access_token'
access_token_secret= 'your_access_token_secret'
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
with open('tweets.csv', 'wb') as csvFile:
tweetWriter = csv.writer(csvFile, delimiter=',')
for i in range(0,5):
tweets = api.search(q='*', lang='en', count=100)
print ("Loop %d" %i)
for tweet in tweets:
tweetData = [tweet.id,tweet.text.encode('utf-8'), tweet.created_at]
tweetWriter.writerow(tweetData)

Leave a Reply

Your email address will not be published. Required fields are marked *

Website Protected by Spam Master