How to Access Data from the Twitter API using Tweepy (Python)
The Twitter API allows you to do many things including retrieve tweet data. In order to access this data, you need a developer account. Using the Twitter API should be an easy thing, but sometimes pictures and simple code can save you some frustration.
This tutorial goes over:
- How to Setup a Twitter Developer Account
- Using tweepy (Python) to Access Twitter Data
How to Setup a Twitter Developer Account
1.) Create a twitter account if you do not have one.
2.) On the twitter developer account page, you will be asked to answer a few questions. For example, I was asked for a phone number, country, and use case. The next step is to read and agree to the developer agreement.
3.) Verify your email.
4.) After verifying your email, you will be sent to a welcome screen. Name your app and click on Get keys.
5.) You now have access to your keys. Make sure to save your information to a secure location. You will need them to access data using the twitter api. This is enough information for OAuth 2.0.
Using tweepy to Access Twitter Data
This section briefly goes over how to use the Python tweepy library to access twitter data. To get started with the library, you need to install it through pip.
pip install tweepy
Search for Tweets from the Last 7 Days
The code below will search and return tweets for the last 7 days with a maximum of 100 tweets per request. This particular code searches for tweets (not retweets) in english that contain the hashtag #petday.
Note that in order to get tweets older than just the last 7 days, you will need to use the search_all_tweets
method which is ONLY available if you upgrade to the academic research product track or other elevated access levels. There is also a good blog on using that method here.
Get More than 100 Tweets at a Time using paginator
If you need more than 100 Tweets, you have to use the paginator method and specify the limit i.e. the total number of Tweets that you want. Replace limit=1000 with the maximum number of tweets you want.
Common Questions + Twitter API Resources
This section aims to provide answers and/or resources to common questions regarding authentication and the twitter API. Naturally, if anyone comments on this blog post, I will do my best to answer the questions here.
This section does its best to provide resources common questions people have with using the Twitter API and tweepy.
What is the difference between OAuth 1.0 vs OAuth 2.0?
There is a good stackoverflow answer on it here. synopsys also has a great article on how the signature workflow works for each.
How do I do things like get a user’s followers, users that like a tweet etc?
There is a comprehensive guide on it by Twitter here.
Conclusion
This tutorial was about getting started with the Twitter API. Future tutorials will go over how to export twitter data as well as sentiment analysis. If you have any questions or thoughts on the tutorial, feel free to reach out in the comments below or through Twitter.
Originally published at https://community.cnvrg.io/.