How to get Tweets from Twitter API in Python
Twitter is a popular social networking service on which users post and interact with messages known as “tweets”. Twitter allows the programmers to extract the tweets of any user using Twitter API or Tweepy.
The first thing to do is to create a Twitter Developer account. This is can done using this. If you have an account, simple login. If don’t just create one.
Next we need to get the consumer key, consumer secret, access key, and access secret. After you receive approval, you need to register your app.
To install Tweepy, use the command:
pip install tweepy
The first step is to import Tweepy and set up authentication and stream listener with API keys.
The below code will generate all the tweets of the particular user and would be appended to the empty array
import tweepy # Function to extract tweets def get_tweets(username): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) number_of_tweets = 100 tweets = api.user_timeline(screen_name=username) arr= tweets_for_csv = [tweet.text for tweet in tweets] for j in tweets_for_csv: arr.append(j) print(arr) if __name__ == '__main__': get_tweets("provide the twitter-handle here")