The 'Get Old Tweets' package is a powerful tool for retrieving historical tweets from Twitter. It provides a way to scrape tweets without needing access to Twitter's official API, making it accessible to everyone. Whether you're a researcher, data analyst, or social media enthusiast, this package is perfect for accessing tweets from specific dates or users.
Installing the 'Get Old Tweets' package is straightforward. You can easily install it using pip with the command
pip install GetOldTweets3. Once installed, you can use it to collect tweets based on various criteria,
such as username, date range, and keywords. This flexibility makes it a valuable resource for accessing Twitter's
rich historical data.
To get started, define your search criteria using the package's built-in methods. For example, you can create a criteria object to fetch tweets from a specific user or within a particular date range. With the getTweets method, you can then retrieve the tweets and process them as needed. This simple yet powerful approach allows
- Retrieve tweets from a specific user within a specified date range.
- Search for tweets containing a specific hashtag or text within a specified date range.
- Fetch tweets based on location.
- Obtain top tweets.
- Access various details of any tweet, such as the username, date, retweets, mentions, hashtags, and more.
limitationoftwitter’sstandardsearchapi
For an overview of the Twitter Search API, visit: Twitter Search API Overview.
There are currently three tiers of the Twitter Search API: Standard, Premium, and Enterprise.
The Standard Twitter Search API is free, but it only allows you to search and fetch tweets from the past 7 days. To access tweets from further back, you need to upgrade to the Premium or Enterprise APIs, which are paid services.
This is where the GetOldTweets package comes in handy. It allows you to freely retrieve tweets that are older than 7 days, making it a valuable tool for accessing historical Twitter data without incurring costs.
Install GetOldTweets
Start by cloning the GetOldTweets repository from GitHub.
After cloning, navigate into the cloned repository directory and execute the following command in your terminal:
By following these steps, you'll have the GetOldTweets package installed and ready to use. This tool is incredibly useful for accessing tweets beyond the seven-day limit imposed by Twitter's free API. With GetOldTweets, you can perform detailed searches and analyses on historical Twitter data without any cost.
pip install -r requirements.txt
This will set up the necessary dependencies for the package.
Get/Search Tweets
Run the Main.py Python script to see example results. This script demonstrates how to fetch tweets
using a specific username or search text.
Below is the full code from the Main.py file:
import sys
if sys.version_info[0] < 3:
import got
else:
import got3 as got
def main():
def printTweet(descr, t):
print(descr)
print("Username: %s" % t.username)
print("Retweets: %d" % t.retweets)
print("Text: %s" % t.text)
print("Mentions: %s" % t.mentions)
print("Hashtags: %s\n" % t.hashtags)
# Example 1 - Get tweets by username
tweetCriteria = got.manager.TweetCriteria().setUsername('mavenbird').setMaxTweets(1)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)[0]
printTweet("### Example 1 - Get tweets by username [mavenbird]", tweet)
# Example 2 - Get tweets by query search
tweetCriteria = got.manager.TweetCriteria().setQuerySearch('europe refugees').setSince("2015-05-01").setUntil("2015-09-30").setMaxTweets(1)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)[0]
printTweet("### Example 2 - Get tweets by query search [europe refugees]", tweet)
# Example 3 - Get tweets by username and bound dates
tweetCriteria = got.manager.TweetCriteria().setUsername("mavenbird").setSince("2015-09-10").setUntil("2015-09-12").setMaxTweets(1)
tweet = got.manager.TweetManager.getTweets(tweetCriteria)[0]
printTweet("### Example 3 - Get tweets by username and bound dates [mavenbird, '2015-09-10', '2015-09-12']", tweet)
if __name__ == '__main__':
main()
Export Tweets to CSV file
This package also includes a feature to export the fetched tweets to a CSV file, which is named
output_got.csv.
The Exporter.py command-line script facilitates this process.
To use this script, you can provide the following attributes:
- username: The username of the Twitter account you want to search (omit the @ symbol).
- since: The start date for your search (format: yyyy-mm-dd).
- until: The end date for your search (format: yyyy-mm-dd).
- querysearch: The text or keywords you want to match in the tweets.
- near: The reference location from which tweets originated.
- within: The radius around the "near" location to search within (e.g., 15mi).
- maxtweets: The maximum number of tweets you want to retrieve.
- toptweets: Retrieve only the tweets flagged as top tweets by Twitter (no additional parameters needed).
- output: The name of the file where results will be exported (default is “output_got.csv”).
# Example 1 - Get tweets by username [mavenbird]
python Exporter.py --username "mavenbird" --maxtweets 1
# Example 2 - Get tweets by query search [europe refugees]
python Exporter.py --querysearch "europe refugees" --maxtweets 1
# Example 3 - Get tweets by username and bound dates [mavenbird, '2015-09-10', '2015-09-12']
python Exporter.py --username "mavenbird" --since 2015-09-10 --until 2015-09-12 --maxtweets 1
# Example 4 - Get the last 10 top tweets by username
python Exporter.py --username "mavenbird" --maxtweets 10 --toptweets