circuit

How to Use Python to Fetch Phone Number Details


At present, it’s normal for everyone to possess a mobile phone. Fundamentally, a mobile phone is used for communication between individuals, where a contact number acts as a 10-digit unique identification key. Contact number is also termed Mobile Subscription Identification Number (MSIN) or Mobile Identification Number (MIN). Users are diverse and have different network carriers, geolocation, time zone, etc.

In this article, you are going to learn about how to fetch the geolocation, carrier, and time zone of a contact number and verify whether it is a valid number or not using Python.

Prerequisite

The first thing needed to start this Python program is to install a Python module. The module required is the phonenumbers module which can be installed using the following command:

pip install phonenumbers

phonenumbers Module

phonenumbers is one of the Python ports from Google’s libphonenumber library. It supports Python versions like Python 2.5 to Python 2.7 and Python 3. It helps in extracting details like time zone, geolocation, network carrier of the contact number, etc. It also helps verify a valid phone number and format numbers to National as well as International format.

Importing phonenumbers Module

The phonenumbers module must be imported first to make use of it in your code. To import use the command as follows:

import phonenumbers
from phonenumbers import carrier, geocoder,timezone

In the above code snippet, only the required submodules like carrier, geocoder and timezone are imported from the module phonenumbers.

Some of the actions to be performed by the phonenumbers module are as follows:

  • Parsing mobile number
  • Fetching timezone of a number
  • Get carrier info of a phone number
  • Extracting geolocation of a mobile number
  • Validating a phone number

Parsing a Mobile Number

Parsing is nothing but a raw data string that is resolved into two components in the format of a phone number like country code and actual contact number. Parsing is accomplished with the help of the parse() method from the phonenumbers module. The raw data given by the user was passed as an argument to the parse() method.

The command to parse a string to contact number format is as follows:

mobileno = input("Enter mobile number with country code: ")
mobileno=phonenumbers.parse(mobileno)
print(mobileno)

The input string entered on the first line of the code gets parsed into a contact number, resulting in two segments as country code and contact number.

Fetching the Time Zone of a Number

The submodule timezone inside the module phonenumbers is used to retrieve the timezone of a particular number. The method time_zones_for_number() is used to get a time zone when the number is passed as an argument.

The command to get the time zone of a number is as follows:

print(timezone.time_zones_for_number(mobileno))

It results in time zones like “(‘Asia/Calcutta’), (‘America/New_York’), (‘Europe/Guernsey’), (‘Europe/Isle_of_Man’), (‘Europe/Jersey’), (‘Europe/London’)”.

Get Carrier Information of a Phone Number

In the context of cellular technology, a wireless carrier or a carrier is a company that provides mobile services. It can also be termed as a mobile network operator, cellular company and wireless service provider.

The carrier name can be fetched with the help of the name_for_number() method in the carrier submodule. This method takes the mobile number and the language code to print out the result.

Command to fetch carrier information of a mobile number is as follows:

print(carrier.name_for_number(mobileno,"en"))

Extracting the Geolocation of a Mobile Number

Geolocation is nothing but the location of the device and the user’s whereabouts. The geographical location of a mobile number is extracted with the help of the description_for_number() method in the geocoder submodule.

The command to fetch geolocation is as follows:

print(geocoder.description_for_number(mobileno,”en”))

This will show the country where the device has been used.

Phone Number Validation

The mobile number given by the user is validated by determining whether it is an exchange-assigned number or a fake number with the help of phonenumbers.is_valid_number() method.

The command to validate a phone number is as follows:

print("Valid mobile number: ", phonenumbers.is_valid_number(mobileno))

Complete Code

The entire code is given below:

import phonenumbers
from phonenumbers import carrier, geocoder, timezone
mobileno = input("Enter mobile number with country code: ")
mobileno=phonenumbers.parse(mobileno)
print(mobileno)
print(timezone.time_zones_for_number(mobileno))
print(carrier.name_for_number(mobileno,"en"))
print(geocoder.description_for_number(mobileno,"en"))
print("Valid mobile number: ",phonenumbers.is_valid_number(mobileno))

Conclusion

Thank you for reading. Hope this article helps in your endeavour.




Continue Learning