Explore Twitter Trends By Location using Python

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:

 
Now, let’s connect to twitter account using the respective key and access token. The details of creating a Twitter Developer account and acquiring access token are here in one of my previous posts.
 
Since we are not supposed to share the key and the secret token, I have created a config file and accessing the details like below:
 
# 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:

 
If we open Twitter, there is an option to set up twitter trends by location. 
 

So, I wanted to explore Twitter Trends from India. I will try to achieve the same requirement using Tweepy. 

 
api.trends_available()[0]
 
trends_available() returns the locations that Twitter has trending topic information for. The response is an array of “locations” that encode the location’s WOEID (a Yahoo! Where On Earth ID) and some other human-readable information such as a canonical name and country the location belongs in. Let’s take a look at the output:
 
 
So, if we are looking for worldwide trends, we need to pass the WOEID = 1. Now, what if we are looking for Trends from India? How to find the WOEID?
 
for val in api.trends_available():
  if val['country'] == 'India':
        print(val.values())
 
If we check the output of the above piece of code, It will return the place name, the place type, the parent id, country, woeid, and country code like below. 
 
 
For India, the woeid is 23424848. Now, let’s write a function fetch WOEID of a place.

 
 

4. Get Twitter Trends by WOEID:

So, now we have the woeid of a place. All we need to see the Twitter Trends for that location. To get the trending topics for a location, we need to pass the woeid to the method trends_place().
 

In the above function, we are passing the woeid of the place and the count of Trends we are looking for. It will return a dataframe with “Trends”, “Volume” and the “Language” of the topic. 

 

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.

 
 
Thank You for Reading!
1

4 comments

  1. 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?

  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *