Scraping Tweets off Twitter with TWINT

Erika D
3 min readDec 15, 2019

--

There are a few ways to collect tweets from Twitter. You can use the Twitter API but the Twitter API limits the number of tweets you can collect. You can manually scrape the tweets you want but this can be time consuming. Another option is to use Twint. Twint is a tool that allows you to scrape Tweets off of Twitter that fit inputted requirements. Twint allows you to search and scrape tweets that contain certain words or phrases, tweets published by specific accounts, tweets within a certain time frame and much more.

Installing & Setting Up Twint

You can install Twint by entering the following command in your terminal.

There are other ways to install Twint and you can read more about the Twint tool and installation here.

After you have installed Twint, you will need to import Twint.

Search Tweets

Some of the search functions available in the TWINT library, are the abilities to search Tweets by username and date. For example, if we wanted to some of the Tweets from Donald Trump after 12/01/2019 we could use the following code:

The code begins with configuring the search requirements and ends with the command to run the Twint search. The outcome is every tweet coming from the username, “realDonaldTrump”. Each tweet returned is after “2019–12–01” and we limited the results to ten tweets. Each instance of a tweet begins with the tweet id, the date the tweet was published, the time the tweet was published, the username the tweet came from, and then the actual written tweet.

We can search all tweets instead of just the tweets from a particular user. For example, we can search tweets that contain a word or phrase. The code below will return the tweets that contain the term “election 2020”. The code will also only return tweets that were publish before December 1, 2019. The code will also only return 10 results.

The tweets being returned are a bit messy to read but we can format how we want the tweets printed. The code below will return the same tweets as the previous code but this time it will only show the tweet id, the date, the time and the actual tweet. We do not see the username because it is not specified. The results will also be formatted as “Tweet id: {id} | Date: {date} | Time: {time} | Tweet: {tweet}”.

You can save your tweets to a csv file as well. The following code is the same as before but it has two additions that allow you to save your tweets in a csv file.

There are a lot more search features to play with within Twint. You can read more about them here.

--

--