Twitter Data

With an API, getting data from Twitter is much easier than scraping it off a webpage.

A common(but not the only) library to use is Tweepy.

We first import the tweepy library, which allows us to fetch Twitter data with a simple API.

import tweepy

Next, you will need to get the API keys from Twitter, and save them to a set of constants. See here for instructions on how to get the keys.

API_KEY =''
API_SECRET =''
ACCESS_TOKEN =''
ACCESS_TOKEN_SECRET =''

Enter the keys above into tweepy’s API.

auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

Decide on the list of users whose feeds you would like to fetch.

users = ['scottadamssays']

Get tweets from the user’s timeline and print out, tweet by tweet.

user_timeline = api.user_timeline(screen_name=users[0], count=10)

for idx, tweet in enumerate(user_timeline):
print(user_timeline[idx].text)

We can also fetch tweets corresponding to specific hashtags in a very similar manner.

topics  ==  ['#infinitywars']

for tweet in tweepy.Cursor(api.search,
q=topics[0],
count=100,
lang='en',
since='2018-01-01').items():
print(tweet.created_at, tweet.text)

The Jupyter notebook with the code is here