Extracting Tweets containing a particular Hashtag using Python
Last Updated : 29 Dec, 2021
Twitter is one of the most popular social media platforms. The Twitter API provides the tools you need to contribute to, engage with, and analyze the conversation happening on Twitter, which finds a lot of application in fields like Data Analytics and Artificial Intelligence. This article focuses on how to extract tweets having a particular Hashtag starting from a given date.
Requirements:
Tweepy is a Python package meant for easy accessing of the Twitter API. Almost all the functionality provided by Twitter API can be used through Tweepy. To install this type the below command in the terminal.
pip install Tweepy
Pandas is a very powerful framework for data analysis in python. It is built on Numpy Package and its key data structure is a DataFrame where one can manipulate tabular data. To install this type the below command in the terminal.
pip install pandas
Prerequisites:
Create a Twitter Developer account and obtain your consumer secret key and access token
Install Tweepy and Pandas module on your system by running this command in Command Prompt
Step-by-step Approach:
Import required modules.
Create an explicit function to display tweet data.
Create another function to scrape data regarding a given Hashtag using tweepy module.
In the Driver Code assign Twitter Developer account credentials along with the Hashtag, initial date and number of tweets.
Finally, call the function to scrape the data with Hashtag, initial date and number of tweets as argument.
Below is the complete program based on the above approach: