How to get Trends for a Certain Location in Twitter using Python

Esraa N.
2 min readNov 22, 2020

Getting the top 50 trends for a location in twitter can help data scientists to analyze the behavior of people at that location. Today I will show you how you can do it in less than 5 minutes.

Prerequisite: Twitter Developer Account

please note that only a certain group of countries are supported. In order to find out the list of supported location you can execute the below in postman to find if the desired location is supported or not

GET https://api.twitter.com/1.1/trends/available.json

For more information, you can check the below link:

If your location is supported you can find the where on earth id (woeid) from

  1. JSON response of the above request: the response will contain the woeid for each place

woeid of Winnipeg = 2972

2. Yahoo weather: you can go to the yahoo weather page for that location and then click on the address bar in the browser. the last digits represent the woeid as shown below

woeid of london = 44418

Now you are ready to get the trends using python:

First, you need to prepare the consumer_key, consumer_secret, access_token and access_token_secret which you can get for the twitter developer portal and import the necessary libraries

import tweepy as tw
import pandas as pd

Second, you will need to authorize the account as shown below:

auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

And at the end, you now can fetch and access the trends

# WOEID of London 
woeid = 44418

# getting the trends
trends = api.trends_place(id = woeid)

# printing the Trends and Their counts
print(“The trends for the location are :”)

for value in trends:
for trend in value[‘trends’]:
print(trend[‘name’], ‘===============> Count =’ , trend[‘tweet_volume’])

--

--

Esraa N.
0 Followers

Phd Student of Computer Science