What is a Twitter Trend?
A trend on Twitter refers to a hashtag-driven topic that is immediately popular at a particular time.
How are trends determined?
A per the Twitter FAQ, Trends are determined by an algorithm and, by default, are tailored for you based on who you follow, your interests, and your location. This algorithm identifies topics that are popular now, rather than topics that have been popular for a while or on a daily basis, to help you discover the hottest emerging topics of discussion on Twitter.
Can we see the Trends for a Particular Location?
Yes, we can see the trends for a particular location that you are interested and that’s what I am planning to do in this post. So, this post is all about exploring twitter trends using Python.
Let’s get started!
1. Import Libraries:
Let’s import all the required libraries first.
# Import Libraries import tweepy ## import csv import sys import config ## Preprocessing import pandas as pd from langdetect import detect
2. Initialize and Connect to Twitter:
# initialize api instance consumer_key= config.consumer_key consumer_secret= config.consumer_secret access_token=config.access_token access_token_secret =config.access_token_secret
#Connect to Twitter through the 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)
3. Get WOEID for a Location:
So, I wanted to explore Twitter Trends from India. I will try to achieve the same requirement using Tweepy.
api.trends_available()[0]
for val in api.trends_available(): if val['country'] == 'India': print(val.values())
4. Get Twitter Trends by WOEID:
The above picture shows TOP10 Trending Hashtags from India. Let’s check twitter for the Trends from India.
Hmmm, so that seems more or less similar, right? Let’s take a look at the Top10 Trends worldwide:
df_world_trends = get_trends_by_location(1, 10)
df_world_trends
Now, let’s take a look at the output:
There are a few Trends in Arabic, which I am not able to read! So, let’s try out the Google Translator to get the translated version.
5. Get the Translated Text of Twitter Trends:
This is just a fun step I wanted to try to get a better understanding of the Trends. For this, I will use googletrans() method.Finally, let’s check the world trends data.
df_world_trends["Translated_Trends"] = [get_translation(val) for val in df_world_trends.Trends]
df_world_trends
Hmm, it’s interesting that #Coronavirus or #Covid19 or #lockdown has taken a back step now. People are discovering more fascinating things to discuss.The python notebook with the code is here.
Andrew
Thank you for posting this!
For step 4, I receive this error: “An exception occurred name ‘detect’ is not defined”. Is there a library I am supposed to install?
Oindrila Sen
You need to include/install the below library if you want to pull the language of the tweets. Check my imports.
from langdetect import detect
Oindrila Sen
Sure! Let me know for which location you are trying to get trends and what’s your error?
Vishal
Hey Oindrilasen,
It was a great blog ,but unfortunately the get twitter trends by WOEID isnt working, it always throws an exception could you please help.