Twitter is an amazing place to explore data as the API is easy to get access to and the data is public available to everyone. This is also the case if you want to plot Tweet Locations on a Leaflet Map using Python.
Using Python to interact with Twitter is easy and does require lot to get started. I prefer to use the tweepy library, which is, as they say, “an easy-to-use Python library to accessing the Twitter API”.
To install the tweepy library, simply type the following in a command shell.
pip install tweepy
The next step is to gather your key values to access the API.
You can get them from https://developer.twitter.com/.
If you need help to get them, I can suggest you follow this tutorial first, which will help you set everything up correctly.
Exploring the data available on tweet, it has a coordinates and place field.
If you read the first word then you realize.
Nullable, which mean that it can be null, i.e., have no value.
But let us see how often they are set.
import tweepy
def get_twitter_api():
# personal details
consumer_key = "___INSERT_YOUR_VALUE_HERE___"
consumer_secret = "___INSERT_YOUR_VALUE_HERE___"
access_token = "___INSERT_YOUR_VALUE_HERE___"
access_token_secret = "___INSERT_YOUR_VALUE_HERE___"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
return api
def get_twitter_location(search):
api = get_twitter_api()
count = 0
for tweet in tweepy.Cursor(api.search, q=search).items(500):
if hasattr(tweet, 'coordinates') and tweet.coordinates is not None:
count += 1
print("Coordinates", tweet.coordinates)
if hasattr(tweet, 'location') and tweet.location is not None:
count += 1
print("Coordinates", tweet.location)
print(count)
get_twitter_location("#100DaysOfCode")
Which resulted in 0. I would not expect this to be the case, but you never know.
Hence, the second best thing you can use, is then the location of the user. Most users have a location given in the user object you see the following.
This results in the following way to collect it. We need to check for the object being None.
def get_tweets(search):
api = get_twitter_api()
location_data = []
for tweet in tweepy.Cursor(api.search, q=search).items(500):
if hasattr(tweet, 'user') and hasattr(tweet.user, 'screen_name') and hasattr(tweet.user, 'location'):
if tweet.user.location:
location_data.append((tweet.user.screen_name, tweet.user.location))
return location_data
Here we collect all the locations of the users of the tweets and return a list of them.
The folium library is amazing to plot data on an interactive leaflet map.
To install the folium library simply type the following command in a terminal.
pip install folium
Or read more here, on how to install it.
We also need to find the coordinates from each location. This can be done by using the library geopy. It can be installed by typing the following command in a terminal.
pip install geopy
Given that the plotting is done by the following lines of code. Please notice, I put a try-except around the geocode call, as it tends to get an timeout.
import folium
from geopy.exc import GeocoderTimedOut
from geopy.geocoders import Nominatim
def put_markers(map, data):
geo_locator = Nominatim(user_agent="LearnPython")
for (name, location) in data:
if location:
try:
location = geo_locator.geocode(location)
except GeocoderTimedOut:
continue
if location:
folium.Marker([location.latitude, location.longitude], popup=name).add_to(map)
if __name__ == "__main__":
map = folium.Map(location=[0, 0], zoom_start=2)
location_data = get_tweets("#100DaysOfCode")
put_markers(map, location_data)
map.save("index.html")
This results in the following beautiful map.
Want to learn more Python? Also, check out my online course on Python.
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…