Home »
Python
Getting old tweets using Getoldtweets3 module in Python
In this article, we will learn how we can access the old tweets using Getoldtweets3 module in Python?
Submitted by Abhinav Gangrade, on July 05, 2020
Library:
GetOldTweets3
GetOldTweets3
GetOldTweets3 is a python module that will help to access the old tweets, as the Twitter API does not allow us to access the tweets of more than 1 week, but with the help of this module, we can access the tweets of any date and any topic and any username.
Code:
# To create a base engine for the tweets:
GetOldTweets3.manager.TweetCriteria()
Code of the username:
# importing the modules
import GetOldTweets3 as gt
# creating a base engine
engine=gt.manager.TweetCriteria()
# storing the tweets for the username
# setusername will set the username
# of the account holder
Tweets=engine.setUsername("Donald Trump").setMaxTweets(3)
# extract the tweeets
# we will take the last third tweet
# gt.manager.TweetManager.getTweets
# will extract the tweets
Tweets_Extracted=gt.manager.TweetManager.getTweets(Tweets)[2]
# print the tweet
print(Tweets_Extracted.text)
Output:
We will get the last third tweet of Donald Trump
You're the best! Sorry for the late reply here.
This poor account is drowning in so much Trump bs,
things meant for me get lost.
Code for the query searching:
# importing the modules
import GetOldTweets3 as gt
# creating a base engine
engine=gt.manager.TweetCriteria()
# storing the tweets for the
# query or topic we want
# setthedate(year-month-day) to set
# untill(year-month-day)
Tweet=engine.setQuerySearch("Corona Virus in india").setSince("2020-04-01").setUntil("2020-06-01").setMaxTweets(2)
# Extract The Tweets
Extracted_Tweets=gt.manager.TweetManager.getTweets(Tweet)[1]
# print the tweet
print(Extracted_Tweets.text)
Output:
Coronavirus live news: Brazil passes 500,000 Covid-19 cases
as India extends lockdown in ‘high-risk’ zones | World news
Code for the username bounded with dates:
# importing the modules
import GetOldTweets3 as gt
# creating a base engine
engine=gt.manager.TweetCriteria()
# storing the tweets for the username
# and setting the dates
# setthedate(year-month-day) to set
# untill(year-month-day)
Tweet=engine.setUsername("narendramodi").setSince("2020-04-01").setUntil("2020-06-01").setMaxTweets(3)
# Extract The Tweets
# extract the third tweet
Extracted_Tweets=gt.manager.TweetManager.getTweets(Tweet)[2]
# print the tweeet
print(Extracted_Tweets.text)
Output:
अंतर्राष्ट्रीय योग दिवस जल्द ही आने वाला है। कोरोना संकट के दौरान लोग योग पर
और अधिक गंभीरता से ध्यान दे रहे हैं। हर जगह लोगों ने योग के साथ-साथ
आयुर्वेद को भी अपनाया है। सही मायने में योग
Community, Immunity और Unity सबके लिए अच्छा है। #MannKiBaat