How to Get Phone Number Information Using Python

How to get phone number information using python

Introduction

Install module

Open your terminal and run the code below.

pip3 install phonenumbers

Create app

Now let’s create a file called app.py and import these modules as shown below.

import phonenumbers
from phonenumbers import geocoder, carrier

Now let’s create a class that will get different information from our phone number such as the phone carrier, region, and validity of the phone number.

class PhoneNumberInfo:
    def __init__(self, number):
        self.number = number
        self.parsed_number = phonenumbers.parse(number, None)

    def is_valid_number(self):
        return phonenumbers.is_valid_number(self.parsed_number)

    def get_region(self):
        return geocoder.description_for_number(self.parsed_number, "en")

    def get_carrier(self):
        return carrier.name_for_number(self.parsed_number, "en")


if __name__ == '__main__':
    number = "+254103425308"
    phone_number_info = PhoneNumberInfo(number)

    print(phone_number_info.parsed_number)
    print("Valid : ", phone_number_info.is_valid_number())
    print("Region : ", phone_number_info.get_region())
    print("Carrier : ", phone_number_info.get_carrier())

Run

Open your terminal and run the command below.

python3 app.py

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *