Social Media Analytics on Trump and Biden’s Twitter
By: Charlie Zheng
Tags: Social Media Analytics, Text Analytics, Data Visualization, Data Analytics

link to my Github for more code: https://github.com/charliezcr/Social-Media-Analytics/blob/main/sma.ipynb

In the recent presidential election, Joe Biden just defeated Donald Trump and became the next president of the United States. The statistics of their use of social media, especially Twitter, are interesting materials to analyze because they provide insights about the presidents’ attitudes and the public’s response. Therefore, on Nov. 9th 2020, after Joe Biden declared his victory, I analyzed the data of their recent tweets by scraping their recent tweets, investigating people’s responses and inspecting the contents of their tweets. Here is the detailed walkthrough of how this social media analytics was conducted.

Twitter Scraping

The first step to obtain the dataset of their tweets by web scraping. Because Twitter disallows web scrapers, you need apply to be a Twitter developer and get consumer key, consumer secret, access token and access token secret to be able to access the data of their twitter account. Aft that, I used Tweepy, a Twitter API to scrape their tweets. Here is the setup of this API.

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True)

This API can be used to query the Twitter library and scrape their tweets. I scraped first 50 tweets according to the time line. Especially, I collected the time stamp, the text of the tweet, the number of retweets and the number of likes of each tweet, among all the attributes of a tweet; and stored the tweets into Pandas Dataframes. Here is the scraping code (I will only use the example of Trump for demo).

# The name of their Twitter accounts

trump = "realDonaldTrump"



# To get 50 tweets

count = 50



# Creation of query method using parameters

trump_tweets = tweepy.Cursor(api.user_timeline,id=trump).items(count)



# Pulling information from tweets iterable object

trump_tweets_list = [[tweet.created_at, tweet.text, tweet.retweet_count, tweet.favorite_count]

for tweet in trump_tweets]



# Creation of dataframe from tweets list

tweets_biden = pd.DataFrame(biden_tweets_list, columns = ["time stamp", "tweet", "retweets","favorites"])

Analysis on Biden and Trump’s tweets

Frequency

By Analyzing the time stamp, we can know how frequent do the presidents tweet.

Biden: the first tweet was sent on 2020-11-08 14:28:23.

the 50th tweet was sent on 2020-11-04 19:10:00.

It took 3 days 19:18:23 for Biden to send 50 tweets. According to the recent data, Biden will send a tweet every 34 minutes.

Trump: the first tweet was sent on 2020-11-09 04:54:16.

the 50th tweet was sent on 2020-11-06 16:37:01.

It took 2 days 12:17:15 for Trump to send 50 tweets. According to the recent data, Trump will send a tweet every 20 minutes.

According to statistics above, both presidents were active on Twitter. They both tweet at a frequently but Trump is more active on Twitter.

Reweets and Favorites

To analyze the public’s response to the presidents’ tweet, I visualized and compared the numbers of retweets and favorites of each tweet by using Python module matplotlib. The plots below demonstrate how welcomed they are on Twitter.