Open In App

How To Track Phone Number Location With Python

Python is a very powerful language and rich in libraries. So the location of a phone number can be traced with Python using a module named 'phone numbers '. This is one of the modules that provide numerous features like basic information about Python a phone number, the region of the phone number, the operator of the phone number, and other carrier information. The phone numbers module also supports the validation and verification of a phone number with other additional features.

How To Track Phone Number Location With Python?

Below, are the methods To Track Phone Number Location With Python.

Install Necessary Packages

We install the required package into our Python environment using the pip command. To install the phone numbers package we use,

pip install phonenumbers

Example 1: Track Phone Number Location Using 'phonenumbers' Package

In this example, below Python code uses the phonenumbers library to extract information from a given phone number. It prompts the user to input a phone number with the country code, then parses and extracts various details such as timezone, geolocation, and service provider. Finally, it prints out these details for the provided number.

import phonenumbers
from phonenumbers import timezone
from phonenumbers import geocoder
from phonenumbers import carrier

#enter phone number along with country code
#Example:+91 8897909599
number = input("Enter phone number with country code : ")
 
# Parsing String to the Phone number
phoneNumber = phonenumbers.parse(number)
 
# printing the timezone using the timezone module
timeZone = timezone.time_zones_for_number(phoneNumber)
print("timezone : "+str(timeZone))
 
# printing the geolocation of the given number using the geocoder module
geolocation = geocoder.description_for_number(phoneNumber,"en")
print("location : "+geolocation)
 
# printing the service provider name using the carrier module
service = carrier.name_for_number(phoneNumber,"en")
print("service provider : "+service)

Output:

phnum

Example 2: Track Phone Number Location Using 'folium', 'opencage' package

In this example, below Python code uses the phonenumbers library to extract information from a given phone number, including the location and service provider. It then uses the OpenCage Geocoding API to fetch the latitude and longitude coordinates corresponding to the location. With this data, it generates a map using Folium library, marks the location on the map, and saves it as an HTML file for visualization.

# track location with the map using the phone number
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
 
import folium
from opencage.geocoder import OpenCageGeocode
 
# taking input the phonenumber along with the country code
#Example: +91 8897909596
number = input("Enter the PhoneNumber with the country code : ")

# Parsing the phonenumber string to convert it into phonenumber format
phoneNumber = phonenumbers.parse(number)
 
# Storing the API Key in the Key variable
#ex-API "45xx61272xxxxd1cb57164b53exxxx"
Key = " Enter your Api" #generate your api https://opencagedata.com/api
 
# Using the geocoder module of phonenumbers to print the Location
yourLocation = geocoder.description_for_number(phoneNumber,"en")
print("Location : "+yourLocation)
 
# Using the carrier module of phonenumbers to print the service provider name
yourServiceProvider = carrier.name_for_number(phoneNumber,"en")
print("service provider : "+yourServiceProvider)
 
# Using opencage to get the latitude and longitude of the location
geocoder = OpenCageGeocode(Key)
query = str(yourLocation)
results = geocoder.geocode(query)
 
# Assigning the latitude and longitude values to the lat and lng variables
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
 
# Getting the map for the given latitude and longitude
myMap = folium.Map(loction=[lat,lng],zoom_start = 9)
 
# Adding a Marker on the map to show the location name
folium.Marker([lat,lng],popup=yourLocation).add_to(myMap)
 
# save map to html file to open it and see the actual location in map format
myMap.save("Location.html")

Output:

phnum2

Locatio in Map:

On opening the 'Location.html' file we can see as below.

lochtml

Video Demonstration

Article Tags :