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?
Can we see the Trends for a Particular Location?
1. Import Libraries:
Let's import all the required libraries first.
import tweepy
##
import csv
import sys
import config
## Preprocessing
import pandas as pd
from langdetect import detect
Initialize and Connect to Twitter:
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:
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?
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
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
No comments:
Post a Comment