Python : Crawling Twitter Trending Topics based on Location using Tweepy


1. Import Library

import sys
import tweepy
import json

Sys
The sys module is used to access the interpreter configuration at runtime and interact with the operating system environment.

Tweepy
The tweepy module is a twitter library which is very useful if used for data mining or knowledge-discovery purposes.

Json
As we know that json is one of the standard data exchange formats between applications. So, I use json as a place to store trending topics data.


2. Define Twitter APIs

We need Twitter APIs to crwaling data on twitter.
A few years ago, Twitter started enforcing policies on the use of Twitter APIs.
To find out more, you can visit this page.

Code to define Twitter APIs :
consumer_key = 'your consumer_key'
consumer_secret = 'your consumer_secret'
access_token = 'your access_token'
access_token_secret = 'your access_token_secret'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

 

3. Define Location and Crawling

Here, I use woe id to define the location to get the trending topics data that I chose.

Code to define location and crawling trending topics:
INDO_WOE_ID = 23424846
indo_trends = api.trends_place(INDO_WOE_ID)
trendings = json.loads(json.dumps(indo_trends, indent=1))

for trending in trendings[0]["trends"]:
    prosescrawl = (trending["name"].strip("#"))
    crawl_trends.append(''.join(prosescrawl))
    print(prosescrawl)

For example, there is trending topics data as a result of crawling that I have gotten:


Thank yo so much..

Comments