Scraping Tweets Directly from Twitter without authentication

Twitter Data
Coding, Data

Scraping Tweets Directly from Twitter without authentication

In fairly recent news, has started indexing it’s entire history of Tweets going all the way back to 2006.

However, even with this news (at time of writing), their search API is still restricted to the past seven days of Tweets.

While I doubt this will be the case permanently, as a useful exercise this presents how we can search for Tweets from Twitter without necessarily using their API.

Besides the indexing, there is also the advantage that Twitter is a little more liberal with rate limits, and you don’t require any authentication keys.

The post will be about looking at what we can extract from Twitter and how we might start to go about it.

Twitters advanced search without being logged

Right, to begin, lets say we want to search Twitter for all tweets related to the query “Babylon 5”. You can access Twitters advanced search without being logged in:

If we take a look at the URL that’s constructed when we perform the search we get:

As we can see, there are two query , q (our query encoded) and src (assumed to be the source of the query, i.e. typed).  However, by default, Twitter returns top results, rather than all, so on the displayed , if you click on All the URL changes to:

The difference here is the f=realtime parameter that appears to specify we receive Tweets in real time as opposed to a subset of top Tweets. Useful to know, but currently we’re only getting the first 25 Tweets back.

If we scroll down though, we notice that more Tweets are loaded on the page via AJAX. Logging all XMLHttpRequests in whatever dev tool you choose to use, we can see that every time we reach the bottom of the page, Twitter makes an AJAX call a URL similar to:

On further inspection, we see that it is also a JSON response, which is very useful! Before we look at the response though, let’s have a look at that URL and some of it’s parameters.

First off, it’s slightly different to the default search URL. The path is /i/search/timeline as opposed to /search. Secondly, while we notice our familiar parameters q, f, and src, from before, there are several additional ones. The most important and essential new one though is scroll_cursor.

This is what Twitter uses to paginate the results. If you remove scroll_cursor from that URL, you end up with your first page of results again.

JSON response provided by Twitter

Now lets take a look now at the JSON response that Twitter provides:

has_more_items: boolean,
items_html: "...",
is_scrolling_request: boolean,
is_refresh_request: boolean,
scroll_cursor: "...",
refresh_cursor: "...",
focused_refresh_interval: int

Again, not all parameters for this post are important to take note of, but the ones that are include: has_more_items, items_html, and scroll_cursor.

has_more_items – This lets you know with a boolean value whether or not there are any more results after this query.

items_html – This is where all the tweets are which Twitter uses to append to the bottom of their timeline. It requires parsing, but there is a good amount of information in there to be extracted which we will look at in a minute.

scroll_cursor – A pagination value that allows us to extract the next page of results.

Make use of the the scroll_cursor parameter

For each search request you make to twitter, the value of this key in the response provides you with the next set of tweets, allowing you to recursively call Twitter until either has_more_items is false, or your previous scroll_cursor equals the last scroll_cursor you had.

Now that we know how to access Twitters own search functionality, lets turn our attention to the tweets themselves.

As mentioned before, items_html in the response is where all the tweets are at. However, it comes in a block of HTML as Twitter injects that block at the bottom of the page each time that call is made.

The HTML inside is a list of li elements, each element a Tweet. I won’t post the HTML for one here, as even one tweet has a lot of HTML in it, but if you want to look at it, copy the items_html value (omiting the quotes around the HTML ) and paste it into something like JSBeautifier to see the formatted results for yourself.

If we look over the HTML, aside from the tweets text, there is actually a lot of useful information encapsulated in this packet. The most important item is the Tweet id itself.

If you check, it’s actually in the root li element. Now, we could stop here as with that ID, you can query Twitters official API, and if it’s a public Tweet, you can get all kinds of information. However, that’d defeat the purpose of not using the API, so lets see what we can extract from what we already have.

Extract with CSS selector queries

The table below shows various CSS selector queries that you can use to extract the information with.

Embedded Tweet Data
Selector Value
div.original-tweet[data-tweet-id] The authors twitter handle
div.original-tweet[data-name] The name of the author
div.original-tweet[data-user-id] The user ID of the author
span._timestamp[data-time] Timestamp of the post
span._timestamp[data-time-ms] Timestamp of the post in ms
p.tweet-text  Text of Tweet
span.ProfileTweet-–retweet > span.ProfileTweet-actionCount[data-tweet-stat-count] Number of Retweets
span.ProfileTweet-action–favorite > span.ProfileTweet-actionCount[data-tweet-stat-count]  Number of Favourites

That’s quite a sizeable amount of information in that HTML. From looking through, we can extract a bunch of stuff about the author, the time stamp of the tweet, the text, and number of retweets and favourites.

To summarize, we know how to construct a Twitter URL query, the response we get from said query, and the information we can extract from said response.

Related posts