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.
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.
Enter the keys above into tweepy’s API.
auth = tweepy.OAuthHandler(API_KEY, API_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, count=10)
for idx, tweet in enumerate(user_timeline):
We can also fetch tweets corresponding to specific hashtags in a very similar manner.
topics == ['#infinitywars']
for tweet in tweepy.Cursor(api.search,
The Jupyter notebook with the code is here