Did you know Python is capable of getting the location of any person just by knowing his/her phone number? In this exercise, we will see how this is possible. This tutorial is only for educational and entertainment purposes.
Did you know Python is capable of getting the location of any person just by knowing his/her phone number? In this exercise, we will see how this is possible. This tutorial is only for educational and entertainment purposes.
The libraries geocoder
, and carrier
libraries from the phonenumbers
library should be imported. These libraries will help with the purpose of this exercise. Let's start!
#Importing libraries
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
#Introducing the phone number to be tracked
number = '+51 XXXXXXXXX'
phone_number = phonenumbers.parse(number)
carrier = carrier.name_for_number(phone_number, 'es')
region = geocoder.description_for_number(phone_number,'es')
#Printing results
print(carrier)
print(phone_number)
If running this code, we will get the following outputs:
As seen from the pictures, Python can output the name of the region in any language. If you want, for example, in English, you write 'en' instead of 'es', or 'de' for German, or 'ru' for Russian. The output of the variable carrier
is the phone company that provides the given phone number.
Python is also able to point on the map the location of the given phone number. How to do so? As you may know, every single location can be defined according to its latitude and longitude coordinates. If you go to Google maps and click on any city, the latitude and longitude coordinates for this specific place will be shown.
However, if you just introduce these numbers into Python, it won't understand. What is the solution then? It is necessary then to get an API key code that will convert the coordinates to a place. We can get the API code by creating an account in OpenCage. Here is the link: https://opencagedata.com/
In Python, there is a library named opencage.geocoder
. As explained, this library and the API key will be used to help Python convert the latitude and longitude coordinates to the corresponding place.
#Importing library
from opencage.geocoder import OpenCageGeocode
#Introducing the API key
key = 'XXXXXXXXXXXXXXXXXXXXXXXX'
geocoder = OpenCageGeocode(key)
query = str(region)
results = geocoder.geocode(query)
print(results)
#Obtaining the latitude and longitude coordinates
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
#Printing the variables lat
and lng
print(lat, lng)
Now you just got the latitude and longitude coordinates. If you copy them into Google Maps, you will be able to see the location! But, you can also 'create' your own map in a nice way in Python. For this purpose, the library folium
is needed. This library makes possible the visualization of the outputted coordinates into an interactive map.
#Importing library
import folium
#Creating the map with the given coordinates
my_map = folium.Map(location=[lat,lng], zoom_start = 5)
#Pointing out on the map the place
folium.Marker([lat,lng], popup = region).add_to((my_map))
Now, it is time to save the map in an html file. This is very simple to do.
#Saving the map in a html file
my_map.save('Location.html')
In the folder you have saved your Python script, you will find a new file, the html one. If you click on it, you will be able to see the place, as shown below.
The final code will look like this:
#Importing libraries
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
from opencage.geocoder import OpenCageGeocode
import folium
#Introducing the phone number to be tracked
number = '+51 XXXXXXXXX'
phone_number = phonenumbers.parse(number)
carrier = carrier.name_for_number(phone_number, 'es')
region = geocoder.description_for_number(phone_number,'es')
#Printing results
print(carrier)
print(phone_number)
#Introducing the API key
key = 'XXXXXXXXXXXXXXXXXXXXXXXX'
geocoder = OpenCageGeocode(key)
query = str(region)
results = geocoder.geocode(query)
print(results)
#Obtaining the latitude and longitude coordinates
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
#Printing the variables lat
and lng
print(lat, lng)
#Creating the map with the given coordinates
my_map = folium.Map(location=[lat,lng], zoom_start = 5)
#Pointing out on the map the place
folium.Marker([lat,lng], popup = region).add_to((my_map))
#Saving the map in a html file
my_map.save('Location.html')
Important:
1. When you enter your desired phone number, please do not forget the country's code. It is very important!
2. Due to companies' security and policy reasons, the location got from the Python script is not accurate.
Views: 1
Notifications
Receive the new articles in your email