Twitter Real Time Streaming API use with Python — Tweet Mining

Retrieving relevant Links to read on ‘Blockchain’ relating to ‘banks’ from real time trending tweets

Mohit Singh
datascape
3 min readApr 16, 2017

--

From a long time I wanted to access twitter real time streaming data and use it in python for analysis. After trying a bunch of libraries, I finally used the Twython library to access Twitter API.

Made a dummy app to generate Twitter API access token and secret key to use in the library.

Generating Twitter API Access Token and Secret Key

After accessing the API through the Twython Library. I wanted to test it, so initially I tried to retrieve tweets from an account. And then I wrote a python script to search the latest real time tweets for a particular keyword and return those tweets. It gives out 100 tweets for access time for like 15 min.

In this example I wanted to retrieve top tweets about blockchain, since its trending these days and also was a topic of discussion for me and my friend for like weeks.

With tweet text, I also retrieve the favorite count, the retweet count and user location to get more insights.

Preview of the Outputs. Left — Relation of retweets and favorite counts. Right — Tweets retrieved about blockchain. Top 100 real time.
How blockchain topic is trending right now on twitter (with respect to retweets)

Now as I have retrieved successfully around 100 tweets about blockchain and stored it into list. I perform text mining over those tweet texts to search for word ‘bank’. For this I had to import python library ‘re’

Now after filtering tweets which involve blockchain and banks. I further mind the tweet text to extract link from it.

The two top links I found trending on twitter about blockchain and banks are: https://t.co/KFFVpaZbRz, https://t.co/fkSPQU61kj, https://t.co/4nTgh9eHhA, https://t.co/1RBIDFgSjc.

This was just an example of how to utilize twitter streaming API using python. Further analysis can be done over the tweets like I am trying sentimental analysis further.

--

--