Last Updated : 24 Mar, 2023
Introduction:
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.
Tweepy is a Python library that provides an easy-to-use interface for accessing Twitter's API. With Tweepy, you can perform various tasks such as searching for tweets, accessing user information, and posting tweets.
To extract tweets using Tweepy, you need to first create a Twitter Developer Account and obtain your API credentials such as consumer key, consumer secret, access token, and access token secret. Once you have your credentials, you can use Tweepy to access Twitter's API and extract tweets.
Steps to obtain keys: - Login to twitter developer section - Go to "Create an App" - Fill the details of the application. - Click on Create your Twitter Application - Details of your new app will be shown along with consumer key and consumer secret. - For access token, click " Create my access token". The page will refresh and generate access token. Tweepy is one of the library that should be installed using pip. Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface. Tweepy provides the convenient Cursor interface to iterate through different types of objects. Twitter allows a maximum of 3200 tweets for extraction. These all are the prerequisite that have to be used before getting tweets of a user. Code(with explanation) :
Python
import tweepy
# Fill the X's with the credentials obtained by
# following the above mentioned procedure.
consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
access_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
access_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# Function to extract tweets
def get_tweets(username):
# Authorization to consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Access to user's access key and access secret
auth.set_access_token(access_key, access_secret)
# Calling api
api = tweepy.API(auth)
# 200 tweets to be extracted
number_of_tweets=200
tweets = api.user_timeline(screen_name=username)
# Empty Array
tmp=[]
# create array of tweet information: username,
# tweet id, date/time, text
tweets_for_csv = [tweet.text for tweet in tweets] # CSV file created
for j in tweets_for_csv:
# Appending tweets to the empty array tmp
tmp.append(j)
# Printing the tweets
print(tmp)
# Driver code
if __name__ == '__main__':
# Here goes the twitter handle for the user
# whose tweets are to be extracted.
get_tweets("twitter-handle")
Steps of Extraction of Tweets using Tweepy :
Here are the step-by-step instructions for extracting tweets using Tweepy:
Uses of Extraction of Tweets :
The extraction of tweets using Tweepy has many potential uses, some of which include:
Conclusion : The above script would generate all the tweets of the particular user and would be appended to the empty array tmp. Here Tweepy is introduced as a tool to access Twitter data in a fairly easy way with Python. There are different types of data we can collect, with the obvious focus on the “tweet” object. Once we have collected some data, the possibilities in terms of analytics applications are endless. One such application of extracting tweets is sentiment or emotion analysis. The emotion of the user can be obtained from the tweets by tokenizing each word and applying machine learning algorithms on that data. Such emotion or sentiment detection is used worldwide and will be broadly used in the future.
How to Extract Tweets using Tweepy
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4