user14816038
user14816038

Reputation: 43

Python easy way to get phone number's carrier

I am looking for a way in python to input a phone number and get a caller's carrier. I am looking for a free and simple way, I have used TELNYX and it returns CELLCO PARTNERSHIP DBA VERIZON instead of just simply 'verizon' which does not work for me. I have tried Twilio as well and it has not worked for me. Has anyone found success doing this? Thanks in advance. Code for the TELNYX:

 def getcarrier(number):
    url = 'https://api.telnyx.com/v1/phone_number/1' + number
    html = requests.get(url).text
    data = json.loads(html)
    data = data["carrier"]
    print(data["name"])
    global carrier

Upvotes: 1

Views: 1505

Answers (1)

exilour
exilour

Reputation: 556

What I have done in the past is to isolate the number prefix. And match against the prefix database available HERE. I did this only for my own country (Bangladesh), so it was a relatively easy code (just a series of if/else). So to work for any number I believe you'll need to consider the country code as well.

You can do it in two ways.

One. Having the data locally stored as CSV from the Wikipedia page. (scraping the page should be easy to do). And then use panda or similar CSV handling package to use it as the database of your program.

Or, two, you can write a small program that scrapes the page on demand and find the operator then.

Good Luck.

Upvotes: 1

Related Questions