Get Phone Number Information using Python

In this Python tutorial, we make a phone number tracker using Python. Learn how to use the Phonenumber library in Python to create a Number tracker that shows you Location and Sim use by the victim or operator.

We will be utilizing the phonenumbers library to obtain information about a phone number.

To begin, run the following line on your command prompt to install the phonenumbers library.

pip install phonenumbers

Convert String to phonenumber format

To investigate the functionalities of the phonenumbers module, we must first obtain a user’s phone number in phonenumber format. In this section, we’ll look at how to convert a user’s phone number to phonenumber format.

The input must be of the string type, and the country code must come before the phone number.

import phonenumbers
pN = phonenumbers.parse("+919876643290")
print(pN)
Country Code: 91 National Number: 9876643290

Get Timezone

Here is a simple Python program that uses the phonenumbers module to determine the timezone of a phone number.

First, we convert the string input to phone number format, and then we utilize an inbuilt method to determine a user’s timezone. It only returns results for valid numbers.

import phonenumbers
from phonenumbers import timezone
pN = phonenumbers.parse("+919876643290")
tZ = timezone.time_zones_for_number(pN)
print(tZ)
('Asia/Calcutta',)

Extract phone numbers from text

Using this module, we can extract phone numbers from text/paragraphs. You may iterate through it to get a list of phone numbers. The PhoneNumberMatcher object offers the necessary method for this.

import phonenumbers
T = "Contact us at +919876643290 or +14691674587"
N = phonenumbers.PhoneNumberMatcher(T, "IN")
for n in N:
    print(n)
PhoneNumberMatch [14,27) +919876643290

Carrier and Region of a Phone Number

We will learn how to use the geocoder and carrier functionalities of this module to determine the carrier and area of a phone number.

import phonenumbers
from phonenumbers import geocoder, carrier
pN = phonenumbers.parse("+919876643290")
C = carrier.name_for_number(pN, 'en')
R = geocoder.description_for_number(pN, 'en')
print(C)
print(R)
Airtel
India

You just learned how to get Phone Number Information in Python. Hope you enjoyed it!

Happy Coding !!!

#python #programming #developer

Get Phone Number Information using Python
2 Likes209.55 GEEK